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 "oic_string.h"
35 #include "cathreadpool.h" /* for thread pool */
37 #include "uarraylist.h"
38 #include "org_iotivity_ca_CaLeClientInterface.h"
40 #define TAG PCF("CA_LE_CLIENT")
42 static const char METHODID_OBJECTNONPARAM[] = "()Landroid/bluetooth/BluetoothAdapter;";
43 static const char CLASSPATH_BT_ADAPTER[] = "android/bluetooth/BluetoothAdapter";
44 static const char CLASSPATH_BT_UUID[] = "java/util/UUID";
45 static const char CLASSPATH_BT_GATT[] = "android/bluetooth/BluetoothGatt";
48 static u_arraylist_t *g_deviceList = NULL; // device list to have same UUID
49 static u_arraylist_t *g_gattObjectList = NULL;
50 static u_arraylist_t *g_deviceStateList = NULL;
52 static CAPacketReceiveCallback g_packetReceiveCallback = NULL;
53 static CABLEErrorHandleCallback g_clientErrorCallback;
54 static ca_thread_pool_t g_threadPoolHandle = NULL;
55 static jobject g_leScanCallback = NULL;
56 static jobject g_leGattCallback = NULL;
57 static jobject g_context = NULL;
58 static jobjectArray g_uuidList = NULL;
60 // it will be prevent to start send logic when adapter has stopped.
61 static bool g_isStartedLEClient = false;
62 static bool g_isStartedMulticastServer = false;
63 static bool g_isStartedScan = false;
65 static jbyteArray g_sendBuffer = NULL;
66 static uint32_t g_targetCnt = 0;
67 static uint32_t g_currentSentCnt = 0;
68 static bool g_isFinishedSendData = false;
69 static ca_mutex g_SendFinishMutex = NULL;
70 static ca_mutex g_threadMutex = NULL;
71 static ca_cond g_threadCond = NULL;
73 static ca_mutex g_threadSendMutex = NULL;
75 static ca_mutex g_bleReqRespClientCbMutex = NULL;
76 static ca_mutex g_bleServerBDAddressMutex = NULL;
78 static ca_mutex g_deviceListMutex = NULL;
79 static ca_mutex g_gattObjectMutex = NULL;
80 static ca_mutex g_deviceStateListMutex = NULL;
82 static CABLEDataReceivedCallback g_CABLEClientDataReceivedCallback = NULL;
85 void CALEClientJniInit()
87 OIC_LOG(DEBUG, TAG, "CALEClientJniInit");
88 g_jvm = (JavaVM*) CANativeJNIGetJavaVM();
91 void CALEClientJNISetContext()
93 OIC_LOG(DEBUG, TAG, "CALEClientJNISetContext");
94 g_context = (jobject) CANativeJNIGetContext();
97 CAResult_t CALECreateJniInterfaceObject()
99 OIC_LOG(DEBUG, TAG, "CALECreateJniInterfaceObject");
103 OIC_LOG(ERROR, TAG, "g_context is null");
104 return CA_STATUS_FAILED;
109 OIC_LOG(ERROR, TAG, "g_jvm is null");
110 return CA_STATUS_FAILED;
113 bool isAttached = false;
115 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
118 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
119 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
123 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
124 return CA_STATUS_FAILED;
129 jclass jni_LEInterface = (*env)->FindClass(env, "org/iotivity/ca/CaLeClientInterface");
130 if (!jni_LEInterface)
132 OIC_LOG(ERROR, TAG, "Could not get CaLeClientInterface class");
136 jmethodID LeInterfaceConstructorMethod = (*env)->GetMethodID(env, jni_LEInterface, "<init>",
137 "(Landroid/content/Context;)V");
138 if (!LeInterfaceConstructorMethod)
140 OIC_LOG(ERROR, TAG, "Could not get CaLeClientInterface constructor method");
144 (*env)->NewObject(env, jni_LEInterface, LeInterfaceConstructorMethod, g_context);
145 OIC_LOG(DEBUG, TAG, "Create instance for CaLeClientInterface");
149 (*g_jvm)->DetachCurrentThread(g_jvm);
158 (*g_jvm)->DetachCurrentThread(g_jvm);
161 return CA_STATUS_FAILED;
164 CAResult_t CALEClientInitialize(ca_thread_pool_t handle)
166 OIC_LOG(DEBUG, TAG, "CALEClientInitialize");
172 OIC_LOG(ERROR, TAG, "g_jvm is null");
173 return CA_STATUS_FAILED;
176 bool isAttached = false;
178 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
181 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
182 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
186 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
187 return CA_STATUS_FAILED;
192 CAResult_t ret = CALECheckPlatformVersion(env, 18);
193 if (CA_STATUS_OK != ret)
195 OIC_LOG(ERROR, TAG, "it is not supported");
199 (*g_jvm)->DetachCurrentThread(g_jvm);
205 g_threadPoolHandle = handle;
207 ret = CALEClientInitGattMutexVaraibles();
208 if (CA_STATUS_OK != ret)
210 OIC_LOG(ERROR, TAG, "CALEClientInitGattMutexVaraibles has failed!");
211 CALEClientTerminateGattMutexVariables();
215 (*g_jvm)->DetachCurrentThread(g_jvm);
221 // init mutex for send logic
222 g_threadCond = ca_cond_new();
224 CALEClientCreateDeviceList();
225 CALEClientJNISetContext();
227 ret = CALEClientCreateUUIDList();
228 if (CA_STATUS_OK != ret)
230 OIC_LOG(ERROR, TAG, "CALEClientCreateUUIDList has failed");
234 (*g_jvm)->DetachCurrentThread(g_jvm);
240 ret = CALECreateJniInterfaceObject(); /* create java caleinterface instance*/
241 if (CA_STATUS_OK != ret)
243 OIC_LOG(ERROR, TAG, "CALECreateJniInterfaceObject has failed");
247 (*g_jvm)->DetachCurrentThread(g_jvm);
252 g_isStartedLEClient = true;
257 void CALEClientTerminate()
259 OIC_LOG(DEBUG, TAG, "CALEClientTerminate");
263 OIC_LOG(ERROR, TAG, "g_jvm is null");
267 bool isAttached = false;
269 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
272 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
273 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
277 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
283 if (g_leScanCallback)
285 (*env)->DeleteGlobalRef(env, g_leScanCallback);
288 if (g_leGattCallback)
290 (*env)->DeleteGlobalRef(env, g_leGattCallback);
295 (*env)->DeleteGlobalRef(env, g_sendBuffer);
300 (*env)->DeleteGlobalRef(env, g_uuidList);
303 CAResult_t ret = CALEClientRemoveAllDeviceState();
304 if (CA_STATUS_OK != ret)
306 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllDeviceState has failed");
309 ret = CALEClientRemoveAllScanDevices(env);
310 if (CA_STATUS_OK != ret)
312 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllScanDevices has failed");
315 ret = CALEClientRemoveAllGattObjs(env);
316 if (CA_STATUS_OK != ret)
318 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllGattObjs has failed");
321 g_isStartedMulticastServer = false;
322 g_isStartedScan = false;
323 CALEClientSetSendFinishFlag(false);
325 CALEClientTerminateGattMutexVariables();
327 ca_cond_free(g_threadCond);
331 (*g_jvm)->DetachCurrentThread(g_jvm);
335 void CALEClientSendFinish(JNIEnv *env, jobject gatt)
337 OIC_LOG(DEBUG, TAG, "CALEClientSendFinish");
338 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
342 CAResult_t res = CALEClientDisconnect(env, gatt);
343 if (CA_STATUS_OK != res)
345 OIC_LOG(ERROR, TAG, "CALEClientDisconnect has failed");
348 CALEClientUpdateSendCnt(env);
351 CAResult_t CALEClientSendUnicastMessage(const char* address,
353 const uint32_t dataLen)
355 OIC_LOG_V(DEBUG, TAG, "CALEClientSendUnicastMessage(%s, %p)", address, data);
356 VERIFY_NON_NULL(address, TAG, "address is null");
357 VERIFY_NON_NULL(data, TAG, "data is null");
359 return CALEClientSendUnicastMessageImpl(address, data, dataLen);
362 CAResult_t CALEClientSendMulticastMessage(const uint8_t* data,
363 const uint32_t dataLen)
365 OIC_LOG_V(DEBUG, TAG, "CALEClientSendMulticastMessage(%p)", data);
366 VERIFY_NON_NULL(data, TAG, "data is null");
370 OIC_LOG(ERROR, TAG, "g_jvm is null");
371 return CA_STATUS_FAILED;
374 bool isAttached = false;
376 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
379 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
380 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
384 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
385 return CA_STATUS_FAILED;
390 CAResult_t ret = CALEClientSendMulticastMessageImpl(env, data, dataLen);
391 if (CA_STATUS_OK != ret)
393 OIC_LOG(ERROR, TAG, "CALEClientSendMulticastMessageImpl has failed");
398 (*g_jvm)->DetachCurrentThread(g_jvm);
404 CAResult_t CALEClientStartUnicastServer(const char* address)
406 OIC_LOG_V(DEBUG, TAG, "it is not needed in this platform (%s)", address);
408 return CA_NOT_SUPPORTED;
411 CAResult_t CALEClientStartMulticastServer()
413 OIC_LOG(DEBUG, TAG, "CALEClientStartMulticastServer");
415 if (g_isStartedMulticastServer)
417 OIC_LOG(ERROR, TAG, "server is already started..it will be skipped");
418 return CA_STATUS_FAILED;
423 OIC_LOG(ERROR, TAG, "g_jvm is null");
424 return CA_STATUS_FAILED;
427 bool isAttached = false;
429 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
432 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
433 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
437 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
438 return CA_STATUS_FAILED;
443 g_isStartedMulticastServer = true;
444 CAResult_t ret = CALEClientStartScan();
445 if (CA_STATUS_OK != ret)
447 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
452 (*g_jvm)->DetachCurrentThread(g_jvm);
458 void CALEClientStopUnicastServer()
460 OIC_LOG(DEBUG, TAG, "CALEClientStopUnicastServer");
463 void CALEClientStopMulticastServer()
465 OIC_LOG(DEBUG, TAG, "CALEClientStopMulticastServer");
466 g_isStartedMulticastServer = false;
467 CAResult_t res = CALEClientStopScan();
468 if (CA_STATUS_OK != res)
470 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
475 void CALEClientSetCallback(CAPacketReceiveCallback callback)
477 g_packetReceiveCallback = callback;
480 void CASetBLEClientErrorHandleCallback(CABLEErrorHandleCallback callback)
482 g_clientErrorCallback = callback;
485 CAResult_t CALEClientSendUnicastMessageImpl(const char* address, const uint8_t* data,
486 const uint32_t dataLen)
488 OIC_LOG_V(DEBUG, TAG, "CALEClientSendUnicastMessageImpl, address: %s, data: %p", address,
490 VERIFY_NON_NULL(address, TAG, "address is null");
491 VERIFY_NON_NULL(data, TAG, "data is null");
495 OIC_LOG(ERROR, TAG, "g_jvm is null");
496 return CA_STATUS_FAILED;
499 bool isAttached = false;
501 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
504 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
505 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
508 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
509 return CA_STATUS_FAILED;
514 ca_mutex_lock(g_threadSendMutex);
516 CAResult_t ret = CA_STATUS_OK;
517 if (g_context && g_deviceList)
519 uint32_t length = u_arraylist_length(g_deviceList);
520 for (uint32_t index = 0; index < length; index++)
522 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
525 OIC_LOG(ERROR, TAG, "jarrayObj is null");
529 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
532 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
536 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
539 OIC_LOG(ERROR, TAG, "setAddress is null");
543 OIC_LOG_V(DEBUG, TAG, "remote device address is %s", setAddress);
545 if (!strcmp(setAddress, address))
547 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
549 // connect to gatt server
550 ret = CALEClientStopScan();
551 if (CA_STATUS_OK != ret)
553 OIC_LOG(ERROR, TAG, "CALEClientStopScan has failed");
559 (*env)->DeleteGlobalRef(env, g_sendBuffer);
561 jbyteArray jni_arr = (*env)->NewByteArray(env, dataLen);
562 (*env)->SetByteArrayRegion(env, jni_arr, 0, dataLen, (jbyte*) data);
563 g_sendBuffer = (jbyteArray)(*env)->NewGlobalRef(env, jni_arr);
565 ret = CALEClientSendData(env, jarrayObj);
566 if (CA_STATUS_OK != ret)
568 OIC_LOG(ERROR, TAG, "CALEClientSendData in unicast is failed");
572 OIC_LOG(INFO, TAG, "wake up");
575 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
581 (*g_jvm)->DetachCurrentThread(g_jvm);
584 ret = CALECheckSendState(address);
585 if(CA_STATUS_OK != ret)
587 OIC_LOG(ERROR, TAG, "send has failed");
591 // start LE Scan again
592 ret = CALEClientStartScan();
593 if (CA_STATUS_OK != ret)
595 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
596 ca_mutex_unlock(g_threadSendMutex);
600 ca_mutex_unlock(g_threadSendMutex);
601 OIC_LOG(INFO, TAG, "unicast - send success");
607 // start LE Scan again
608 ret = CALEClientStartScan();
609 if (CA_STATUS_OK != ret)
611 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
612 ca_mutex_unlock(g_threadSendMutex);
618 (*g_jvm)->DetachCurrentThread(g_jvm);
620 ca_mutex_unlock(g_threadSendMutex);
621 return CA_SEND_FAILED;
624 CAResult_t CALEClientSendMulticastMessageImpl(JNIEnv *env, const uint8_t* data,
625 const uint32_t dataLen)
627 OIC_LOG_V(DEBUG, TAG, "CASendMulticastMessageImpl, send to, data: %p, %u", data, dataLen);
628 VERIFY_NON_NULL(data, TAG, "data is null");
629 VERIFY_NON_NULL(env, TAG, "env is null");
633 OIC_LOG(ERROR, TAG, "g_deviceList is null");
634 return CA_STATUS_FAILED;
637 ca_mutex_lock(g_threadSendMutex);
639 CALEClientSetSendFinishFlag(false);
641 OIC_LOG(DEBUG, TAG, "set byteArray for data");
644 (*env)->DeleteGlobalRef(env, g_sendBuffer);
646 jbyteArray jni_arr = (*env)->NewByteArray(env, dataLen);
647 (*env)->SetByteArrayRegion(env, jni_arr, 0, dataLen, (jbyte*) data);
648 g_sendBuffer = (jbyteArray)(*env)->NewGlobalRef(env, jni_arr);
650 // connect to gatt server
651 CAResult_t res = CALEClientStopScan();
652 if (CA_STATUS_OK != res)
654 OIC_LOG(ERROR, TAG, "CALEClientStopScan has failed");
655 ca_mutex_unlock(g_threadSendMutex);
659 uint32_t length = u_arraylist_length(g_deviceList);
660 g_targetCnt = length;
666 for (uint32_t index = 0; index < length; index++)
668 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
671 OIC_LOG(ERROR, TAG, "jarrayObj is not available");
675 res = CALEClientSendData(env, jarrayObj);
676 if (res != CA_STATUS_OK)
678 OIC_LOG(ERROR, TAG, "BT device - send has failed");
681 jstring jni_address = CALEGetAddressFromBTDevice(env, jarrayObj);
684 OIC_LOG(ERROR, TAG, "CALEGetAddressFromBTDevice has failed");
688 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
691 OIC_LOG(ERROR, TAG, "address is not available");
695 res = CALECheckSendState(address);
696 if (CA_STATUS_OK != res)
698 OIC_LOG_V(INFO, TAG, "multicast : send has failed for this device[%s]", address);
699 g_clientErrorCallback(address, data, dataLen, res);
700 (*env)->ReleaseStringUTFChars(env, jni_address, address);
704 (*env)->ReleaseStringUTFChars(env, jni_address, address);
707 OIC_LOG(DEBUG, TAG, "connection routine is finished");
709 // wait for finish to send data through "CALeGattServicesDiscoveredCallback"
710 if (!g_isFinishedSendData)
712 ca_mutex_lock(g_threadMutex);
713 ca_cond_wait(g_threadCond, g_threadMutex);
714 OIC_LOG(DEBUG, TAG, "the data was sent for All devices");
715 ca_mutex_unlock(g_threadMutex);
718 // start LE Scan again
719 res = CALEClientStartScan();
720 if (CA_STATUS_OK != res)
722 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
723 ca_mutex_unlock(g_threadSendMutex);
727 ca_mutex_unlock(g_threadSendMutex);
728 OIC_LOG(DEBUG, TAG, "OUT - CALEClientSendMulticastMessageImpl");
732 res = CALEClientStartScan();
733 if (CA_STATUS_OK != res)
735 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
736 ca_mutex_unlock(g_threadSendMutex);
740 ca_mutex_unlock(g_threadSendMutex);
741 OIC_LOG(DEBUG, TAG, "OUT - CALEClientSendMulticastMessageImpl");
742 return CA_SEND_FAILED;
745 CAResult_t CALECheckSendState(const char* address)
747 VERIFY_NON_NULL(address, TAG, "address is null");
749 ca_mutex_lock(g_deviceStateListMutex);
750 CALEState_t* state = CALEClientGetStateInfo(address);
753 OIC_LOG(ERROR, TAG, "state is null");
754 ca_mutex_unlock(g_deviceStateListMutex);
755 return CA_SEND_FAILED;
758 if (STATE_SEND_SUCCESS != state->sendState)
760 OIC_LOG(ERROR, TAG, "sendstate is not STATE_SEND_SUCCESS");
761 ca_mutex_unlock(g_deviceStateListMutex);
762 return CA_SEND_FAILED;
764 ca_mutex_unlock(g_deviceStateListMutex);
768 CAResult_t CALEClientSendData(JNIEnv *env, jobject device)
770 OIC_LOG(DEBUG, TAG, "IN - CALEClientSendData");
771 VERIFY_NON_NULL(device, TAG, "device is null");
772 VERIFY_NON_NULL(env, TAG, "env is null");
774 jstring jni_address = CALEGetAddressFromBTDevice(env, device);
777 OIC_LOG(ERROR, TAG, "CALEGetAddressFromBTDevice has failed");
778 return CA_STATUS_FAILED;
781 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
784 OIC_LOG(ERROR, TAG, "address is not available");
785 return CA_STATUS_FAILED;
788 ca_mutex_lock(g_deviceStateListMutex);
789 CALEState_t* state = CALEClientGetStateInfo(address);
790 ca_mutex_unlock(g_deviceStateListMutex);
793 OIC_LOG(DEBUG, TAG, "state is empty..start to connect LE");
794 CAResult_t ret = CALEClientConnect(env, device, JNI_FALSE, g_leGattCallback);
795 if (CA_STATUS_OK != ret)
797 OIC_LOG(ERROR, TAG, "CALEClientConnect has failed");
798 (*env)->ReleaseStringUTFChars(env, jni_address, address);
804 if (STATE_CONNECTED == state->connectedState)
806 OIC_LOG(INFO, TAG, "GATT has already connected");
807 jobject gatt = CALEClientGetGattObjInList(env, address);
810 OIC_LOG(ERROR, TAG, "CALEClientGetGattObjInList has failed");
811 (*env)->ReleaseStringUTFChars(env, jni_address, address);
812 return CA_STATUS_FAILED;
815 CAResult_t ret = CALEClientWriteCharacteristic(env, gatt);
816 if (CA_STATUS_OK != ret)
818 OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
819 (*env)->ReleaseStringUTFChars(env, jni_address, address);
825 OIC_LOG(DEBUG, TAG, "start to connect LE");
826 CAResult_t ret = CALEClientConnect(env, device, JNI_FALSE, g_leGattCallback);
827 if (CA_STATUS_OK != ret)
829 OIC_LOG(ERROR, TAG, "CALEClientConnect has failed");
830 (*env)->ReleaseStringUTFChars(env, jni_address, address);
836 (*env)->ReleaseStringUTFChars(env, jni_address, address);
840 jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt)
842 VERIFY_NON_NULL_RET(gatt, TAG, "gatt is null", NULL);
843 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
845 jclass jni_cid_gattdevice_list = (*env)->FindClass(env, CLASSPATH_BT_GATT);
846 if (!jni_cid_gattdevice_list)
848 OIC_LOG(ERROR, TAG, "jni_cid_gattdevice_list is null");
852 jmethodID jni_mid_getDevice = (*env)->GetMethodID(env, jni_cid_gattdevice_list, "getDevice",
853 "()Landroid/bluetooth/BluetoothDevice;");
854 if (!jni_mid_getDevice)
856 OIC_LOG(ERROR, TAG, "jni_mid_getDevice is null");
860 jobject jni_obj_device = (*env)->CallObjectMethod(env, gatt, jni_mid_getDevice);
863 OIC_LOG(ERROR, TAG, "jni_obj_device is null");
867 jstring jni_address = CALEGetAddressFromBTDevice(env, jni_obj_device);
870 OIC_LOG(ERROR, TAG, "jni_address is null");
880 CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt)
883 OIC_LOG(DEBUG, TAG, "Gatt Close");
884 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
885 VERIFY_NON_NULL(env, TAG, "env is null");
887 // get BluetoothGatt class
888 OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
889 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
890 if (!jni_cid_BluetoothGatt)
892 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
893 return CA_STATUS_FAILED;
896 jmethodID jni_mid_closeGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "close", "()V");
897 if (!jni_mid_closeGatt)
899 OIC_LOG(ERROR, TAG, "jni_mid_closeGatt is null");
903 // call disconnect gatt method
904 OIC_LOG(DEBUG, TAG, "request to close GATT");
905 (*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_closeGatt);
907 if ((*env)->ExceptionCheck(env))
909 OIC_LOG(ERROR, TAG, "closeGATT has failed");
910 (*env)->ExceptionDescribe(env);
911 (*env)->ExceptionClear(env);
912 return CA_STATUS_FAILED;
918 CAResult_t CALEClientStartScan()
920 if (!g_isStartedMulticastServer)
922 OIC_LOG(ERROR, TAG, "server is not started yet..scan will be passed");
923 return CA_STATUS_FAILED;
926 if (!g_isStartedLEClient)
928 OIC_LOG(ERROR, TAG, "LE client is not started");
929 return CA_STATUS_FAILED;
934 OIC_LOG(ERROR, TAG, "g_jvm is null");
935 return CA_STATUS_FAILED;
940 OIC_LOG(INFO, TAG, "scanning is already started");
944 bool isAttached = false;
946 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
949 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
951 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
954 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
955 return CA_STATUS_FAILED;
960 OIC_LOG(DEBUG, TAG, "CALEClientStartScan");
962 CAResult_t ret = CA_STATUS_OK;
963 // scan gatt server with UUID
964 if (g_leScanCallback && g_uuidList)
967 ret = CALEClientStartScanWithUUIDImpl(env, g_uuidList, g_leScanCallback);
968 if(CA_STATUS_OK != ret)
970 OIC_LOG(ERROR, TAG, "CALEClientStartScanWithUUIDImpl has failed");
973 ret = CALEClientStartScanImpl(env, g_leScanCallback);
974 if (CA_STATUS_OK != ret)
976 OIC_LOG(ERROR, TAG, "CALEClientStartScanImpl has failed");
983 (*g_jvm)->DetachCurrentThread(g_jvm);
989 CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback)
991 VERIFY_NON_NULL(callback, TAG, "callback is null");
992 VERIFY_NON_NULL(env, TAG, "env is null");
994 if (!CALEIsEnableBTAdapter(env))
996 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
997 return CA_ADAPTER_NOT_ENABLED;
1000 // get default bt adapter class
1001 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
1002 if (!jni_cid_BTAdapter)
1004 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_cid_BTAdapter is null");
1005 return CA_STATUS_FAILED;
1008 // get remote bt adapter method
1009 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
1010 "getDefaultAdapter",
1011 METHODID_OBJECTNONPARAM);
1012 if (!jni_mid_getDefaultAdapter)
1014 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
1015 return CA_STATUS_FAILED;
1018 // get start le scan method
1019 jmethodID jni_mid_startLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "startLeScan",
1020 "(Landroid/bluetooth/BluetoothAdapter$"
1021 "LeScanCallback;)Z");
1022 if (!jni_mid_startLeScan)
1024 OIC_LOG(ERROR, TAG, "startLeScan: jni_mid_startLeScan is null");
1025 return CA_STATUS_FAILED;
1028 // gat bt adapter object
1029 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
1030 jni_mid_getDefaultAdapter);
1031 if (!jni_obj_BTAdapter)
1033 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_obj_BTAdapter is null");
1034 return CA_STATUS_FAILED;
1037 // call start le scan method
1038 jboolean jni_obj_startLeScan = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter,
1039 jni_mid_startLeScan, callback);
1040 if (!jni_obj_startLeScan)
1042 OIC_LOG(ERROR, TAG, "startLeScan is failed");
1043 return CA_STATUS_FAILED;
1047 OIC_LOG(DEBUG, TAG, "startLeScan is started");
1048 g_isStartedScan = true;
1051 return CA_STATUS_OK;
1054 CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids, jobject callback)
1056 VERIFY_NON_NULL(callback, TAG, "callback is null");
1057 VERIFY_NON_NULL(uuids, TAG, "uuids is null");
1058 VERIFY_NON_NULL(env, TAG, "env is null");
1060 if (!CALEIsEnableBTAdapter(env))
1062 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1063 return CA_ADAPTER_NOT_ENABLED;
1066 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
1067 if (!jni_cid_BTAdapter)
1069 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_cid_BTAdapter is null");
1070 return CA_STATUS_FAILED;
1073 // get remote bt adapter method
1074 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
1075 "getDefaultAdapter",
1076 METHODID_OBJECTNONPARAM);
1077 if (!jni_mid_getDefaultAdapter)
1079 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
1080 return CA_STATUS_FAILED;
1083 // get start le scan method
1084 jmethodID jni_mid_startLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "startLeScan",
1085 "([Ljava/util/UUID;Landroid/bluetooth/"
1086 "BluetoothAdapter$LeScanCallback;)Z");
1087 if (!jni_mid_startLeScan)
1089 OIC_LOG(ERROR, TAG, "startLeScan: jni_mid_startLeScan is null");
1090 return CA_STATUS_FAILED;
1093 // get bt adapter object
1094 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
1095 jni_mid_getDefaultAdapter);
1096 if (!jni_obj_BTAdapter)
1098 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_obj_BTAdapter is null");
1099 return CA_STATUS_FAILED;
1102 // call start le scan method
1103 jboolean jni_obj_startLeScan = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter,
1104 jni_mid_startLeScan, uuids, callback);
1105 if (!jni_obj_startLeScan)
1107 OIC_LOG(ERROR, TAG, "startLeScan With UUID is failed");
1108 return CA_STATUS_FAILED;
1112 OIC_LOG(DEBUG, TAG, "startLeScan With UUID is started");
1113 g_isStartedScan = true;
1116 return CA_STATUS_OK;
1119 jobject CALEClientGetUUIDObject(JNIEnv *env, const char* uuid)
1121 VERIFY_NON_NULL_RET(uuid, TAG, "uuid is null", NULL);
1122 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
1125 jclass jni_cid_uuid = (*env)->FindClass(env, CLASSPATH_BT_UUID);
1128 OIC_LOG(ERROR, TAG, "jni_cid_uuid is null");
1132 jmethodID jni_mid_fromString = (*env)->GetStaticMethodID(env, jni_cid_uuid, "fromString",
1133 "(Ljava/lang/String;)"
1134 "Ljava/util/UUID;");
1135 if (!jni_mid_fromString)
1137 OIC_LOG(ERROR, TAG, "jni_mid_fromString is null");
1141 jstring jni_uuid = (*env)->NewStringUTF(env, uuid);
1142 jobject jni_obj_uuid = (*env)->CallStaticObjectMethod(env, jni_cid_uuid, jni_mid_fromString,
1146 OIC_LOG(ERROR, TAG, "jni_obj_uuid is null");
1150 return jni_obj_uuid;
1153 CAResult_t CALEClientStopScan()
1157 OIC_LOG(ERROR, TAG, "g_jvm is null");
1158 return CA_STATUS_FAILED;
1161 if (!g_isStartedScan)
1163 OIC_LOG(INFO, TAG, "scanning is already stopped");
1164 return CA_STATUS_OK;
1167 bool isAttached = false;
1169 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1172 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
1173 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1176 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1177 return CA_STATUS_FAILED;
1182 CAResult_t ret = CALEClientStopScanImpl(env, g_leScanCallback);
1183 if (CA_STATUS_OK != ret)
1185 OIC_LOG(ERROR, TAG, "CALEClientStopScanImpl has failed");
1189 g_isStartedScan = false;
1194 (*g_jvm)->DetachCurrentThread(g_jvm);
1200 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback)
1202 OIC_LOG(DEBUG, TAG, "CALEClientStopScanImpl");
1203 VERIFY_NON_NULL(callback, TAG, "callback is null");
1204 VERIFY_NON_NULL(env, TAG, "env is null");
1206 if (!CALEIsEnableBTAdapter(env))
1208 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1209 return CA_ADAPTER_NOT_ENABLED;
1212 // get default bt adapter class
1213 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
1214 if (!jni_cid_BTAdapter)
1216 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_cid_BTAdapter is null");
1217 return CA_STATUS_FAILED;
1220 // get remote bt adapter method
1221 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
1222 "getDefaultAdapter",
1223 METHODID_OBJECTNONPARAM);
1224 if (!jni_mid_getDefaultAdapter)
1226 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
1227 return CA_STATUS_FAILED;
1230 // get start le scan method
1231 jmethodID jni_mid_stopLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "stopLeScan",
1232 "(Landroid/bluetooth/"
1233 "BluetoothAdapter$LeScanCallback;)V");
1234 if (!jni_mid_stopLeScan)
1236 OIC_LOG(ERROR, TAG, "stopLeScan: jni_mid_stopLeScan is null");
1237 return CA_STATUS_FAILED;
1240 // gat bt adapter object
1241 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
1242 jni_mid_getDefaultAdapter);
1243 if (!jni_obj_BTAdapter)
1245 OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
1246 return CA_STATUS_FAILED;
1249 OIC_LOG(DEBUG, TAG, "CALL API - request to stop LE Scan");
1250 // call start le scan method
1251 (*env)->CallVoidMethod(env, jni_obj_BTAdapter, jni_mid_stopLeScan, callback);
1252 if ((*env)->ExceptionCheck(env))
1254 OIC_LOG(ERROR, TAG, "stopLeScan has failed");
1255 (*env)->ExceptionDescribe(env);
1256 (*env)->ExceptionClear(env);
1257 return CA_STATUS_FAILED;
1260 return CA_STATUS_OK;
1263 CAResult_t CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect,
1266 OIC_LOG(DEBUG, TAG, "GATT CONNECT");
1267 VERIFY_NON_NULL(env, TAG, "env is null");
1268 VERIFY_NON_NULL(bluetoothDevice, TAG, "bluetoothDevice is null");
1269 VERIFY_NON_NULL(callback, TAG, "callback is null");
1271 if (!CALEIsEnableBTAdapter(env))
1273 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1274 return CA_ADAPTER_NOT_ENABLED;
1277 jstring jni_address = CALEGetAddressFromBTDevice(env, bluetoothDevice);
1280 OIC_LOG(ERROR, TAG, "bleConnect: CALEGetAddressFromBTDevice is null");
1281 return CA_STATUS_FAILED;
1284 // get BluetoothDevice class
1285 OIC_LOG(DEBUG, TAG, "get BluetoothDevice class");
1286 jclass jni_cid_BluetoothDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
1287 if (!jni_cid_BluetoothDevice)
1289 OIC_LOG(ERROR, TAG, "bleConnect: jni_cid_BluetoothDevice is null");
1290 return CA_STATUS_FAILED;
1293 // get connectGatt method
1294 OIC_LOG(DEBUG, TAG, "get connectGatt method");
1295 jmethodID jni_mid_connectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothDevice, "connectGatt",
1296 "(Landroid/content/Context;ZLandroid/"
1297 "bluetooth/BluetoothGattCallback;)"
1298 "Landroid/bluetooth/BluetoothGatt;");
1299 if (!jni_mid_connectGatt)
1301 OIC_LOG(ERROR, TAG, "bleConnect: jni_mid_connectGatt is null");
1302 return CA_STATUS_FAILED;
1305 OIC_LOG(DEBUG, TAG, "Call object method - connectGatt");
1306 jobject jni_obj_connectGatt = (*env)->CallObjectMethod(env, bluetoothDevice,
1307 jni_mid_connectGatt,
1309 autoconnect, callback);
1310 if (!jni_obj_connectGatt)
1312 OIC_LOG(ERROR, TAG, "CALL API - connectGatt was failed..it will be removed");
1313 CALEClientRemoveDeviceInScanDeviceList(env, jni_address);
1314 CALEClientUpdateSendCnt(env);
1315 return CA_STATUS_FAILED;
1319 OIC_LOG(DEBUG, TAG, "le connecting..please wait..");
1321 return CA_STATUS_OK;
1324 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt)
1326 OIC_LOG(DEBUG, TAG, "GATT DISCONNECT");
1327 VERIFY_NON_NULL(env, TAG, "env is null");
1328 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1330 if (!CALEIsEnableBTAdapter(env))
1332 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1333 return CA_ADAPTER_NOT_ENABLED;
1336 // get BluetoothGatt class
1337 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1338 if (!jni_cid_BluetoothGatt)
1340 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1341 return CA_STATUS_FAILED;
1344 OIC_LOG(DEBUG, TAG, "get gatt disconnect method");
1345 jmethodID jni_mid_disconnectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1346 "disconnect", "()V");
1347 if (!jni_mid_disconnectGatt)
1349 OIC_LOG(ERROR, TAG, "jni_mid_disconnectGatt is null");
1350 return CA_STATUS_FAILED;
1353 // call disconnect gatt method
1354 (*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_disconnectGatt);
1355 if ((*env)->ExceptionCheck(env))
1357 OIC_LOG(ERROR, TAG, "disconnect has failed");
1358 (*env)->ExceptionDescribe(env);
1359 (*env)->ExceptionClear(env);
1360 return CA_STATUS_FAILED;
1363 OIC_LOG(DEBUG, TAG, "disconnecting Gatt...");
1365 return CA_STATUS_OK;
1368 CAResult_t CALEClientDisconnectAll(JNIEnv *env)
1370 OIC_LOG(DEBUG, TAG, "CALEClientDisconnectAll");
1371 VERIFY_NON_NULL(env, TAG, "env is null");
1373 if (!g_gattObjectList)
1375 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
1376 return CA_STATUS_FAILED;
1379 uint32_t length = u_arraylist_length(g_gattObjectList);
1380 for (uint32_t index = 0; index < length; index++)
1382 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
1385 OIC_LOG(ERROR, TAG, "jarrayObj is null");
1388 CAResult_t res = CALEClientDisconnect(env, jarrayObj);
1389 if (CA_STATUS_OK != res)
1391 OIC_LOG(ERROR, TAG, "CALEClientDisconnect has failed");
1396 OICFree(g_gattObjectList);
1397 g_gattObjectList = NULL;
1399 return CA_STATUS_OK;
1402 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt)
1404 VERIFY_NON_NULL(env, TAG, "env is null");
1405 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1407 if (!CALEIsEnableBTAdapter(env))
1409 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1410 return CA_ADAPTER_NOT_ENABLED;
1413 // get BluetoothGatt class
1414 OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
1415 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1416 if (!jni_cid_BluetoothGatt)
1418 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1419 return CA_STATUS_FAILED;
1422 OIC_LOG(DEBUG, TAG, "discovery gatt services method");
1423 jmethodID jni_mid_discoverServices = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1424 "discoverServices", "()Z");
1425 if (!jni_mid_discoverServices)
1427 OIC_LOG(ERROR, TAG, "jni_mid_discoverServices is null");
1428 return CA_STATUS_FAILED;
1430 // call disconnect gatt method
1431 OIC_LOG(DEBUG, TAG, "CALL API - request discovery gatt services");
1432 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_discoverServices);
1435 OIC_LOG(ERROR, TAG, "discoverServices has not been started");
1436 return CA_STATUS_FAILED;
1439 return CA_STATUS_OK;
1442 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt)
1444 VERIFY_NON_NULL(env, TAG, "env is null");
1445 VERIFY_NON_NULL(gatt, TAG, "gatt is null");
1448 jobject jni_obj_character = CALEClientCreateGattCharacteristic(env, gatt, g_sendBuffer);
1449 if (!jni_obj_character)
1451 CALEClientSendFinish(env, gatt);
1452 return CA_STATUS_FAILED;
1455 CAResult_t ret = CALEClientWriteCharacteristicImpl(env, gatt, jni_obj_character);
1456 if (CA_STATUS_OK != ret)
1458 CALEClientSendFinish(env, gatt);
1462 return CA_STATUS_OK;
1465 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
1466 jobject gattCharacteristic)
1468 OIC_LOG(DEBUG, TAG, "WRITE GATT CHARACTERISTIC");
1469 VERIFY_NON_NULL(env, TAG, "env is null");
1470 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1471 VERIFY_NON_NULL(gattCharacteristic, TAG, "gattCharacteristic is null");
1473 if (!CALEIsEnableBTAdapter(env))
1475 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1476 return CA_STATUS_FAILED;
1479 // get BluetoothGatt class
1480 OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
1481 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1482 if (!jni_cid_BluetoothGatt)
1484 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1485 return CA_STATUS_FAILED;
1488 OIC_LOG(DEBUG, TAG, "write characteristic method");
1489 jmethodID jni_mid_writeCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1490 "writeCharacteristic",
1491 "(Landroid/bluetooth/"
1492 "BluetoothGattCharacteristic;)Z");
1493 if (!jni_mid_writeCharacteristic)
1495 OIC_LOG(ERROR, TAG, "jni_mid_writeCharacteristic is null");
1496 return CA_STATUS_FAILED;
1499 // call disconnect gatt method
1500 OIC_LOG(DEBUG, TAG, "CALL API - request to write gatt characteristic");
1501 jboolean ret = (jboolean)(*env)->CallBooleanMethod(env, bluetoothGatt,
1502 jni_mid_writeCharacteristic,
1503 gattCharacteristic);
1506 OIC_LOG(DEBUG, TAG, "writeCharacteristic success");
1510 OIC_LOG(ERROR, TAG, "writeCharacteristic has failed");
1511 return CA_STATUS_FAILED;
1514 return CA_STATUS_OK;
1517 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt)
1519 VERIFY_NON_NULL(env, TAG, "env is null");
1520 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1522 if (!CALEIsEnableBTAdapter(env))
1524 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1525 return CA_STATUS_FAILED;
1528 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1529 if (!jni_cid_BluetoothGatt)
1531 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1532 return CA_STATUS_FAILED;
1535 jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_RESPONSE_UUID);
1538 OIC_LOG(ERROR, TAG, "jni_uuid is null");
1539 return CA_STATUS_FAILED;
1542 jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, bluetoothGatt, jni_uuid);
1543 if (!jni_obj_GattCharacteristic)
1545 OIC_LOG(ERROR, TAG, "jni_obj_GattCharacteristic is null");
1546 return CA_STATUS_FAILED;
1549 OIC_LOG(DEBUG, TAG, "read characteristic method");
1550 jmethodID jni_mid_readCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1551 "readCharacteristic",
1552 "(Landroid/bluetooth/"
1553 "BluetoothGattCharacteristic;)Z");
1554 if (!jni_mid_readCharacteristic)
1556 OIC_LOG(ERROR, TAG, "jni_mid_readCharacteristic is null");
1557 return CA_STATUS_FAILED;
1560 // call disconnect gatt method
1561 OIC_LOG(DEBUG, TAG, "CALL API - request to read gatt characteristic");
1562 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_readCharacteristic,
1563 jni_obj_GattCharacteristic);
1566 OIC_LOG(DEBUG, TAG, "readCharacteristic success");
1570 OIC_LOG(ERROR, TAG, "readCharacteristic has failed");
1571 return CA_STATUS_FAILED;
1574 return CA_STATUS_OK;
1577 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
1578 jobject characteristic)
1580 VERIFY_NON_NULL(env, TAG, "env is null");
1581 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1582 VERIFY_NON_NULL(characteristic, TAG, "characteristic is null");
1584 if (!CALEIsEnableBTAdapter(env))
1586 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1587 return CA_ADAPTER_NOT_ENABLED;
1590 // get BluetoothGatt class
1591 OIC_LOG(DEBUG, TAG, "CALEClientSetCharacteristicNotification");
1592 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1593 if (!jni_cid_BluetoothGatt)
1595 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1596 return CA_STATUS_FAILED;
1599 // set Characteristic Notification
1600 jmethodID jni_mid_setNotification = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1601 "setCharacteristicNotification",
1602 "(Landroid/bluetooth/"
1603 "BluetoothGattCharacteristic;Z)Z");
1604 if (!jni_mid_setNotification)
1606 OIC_LOG(ERROR, TAG, "jni_mid_getService is null");
1607 return CA_STATUS_FAILED;
1610 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_setNotification,
1611 characteristic, JNI_TRUE);
1612 if (JNI_TRUE == ret)
1614 OIC_LOG(DEBUG, TAG, "CALL API - setCharacteristicNotification success");
1618 OIC_LOG(ERROR, TAG, "CALL API - setCharacteristicNotification has failed");
1619 return CA_STATUS_FAILED;
1622 return CA_STATUS_OK;
1625 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID)
1627 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
1628 VERIFY_NON_NULL_RET(bluetoothGatt, TAG, "bluetoothGatt is null", NULL);
1629 VERIFY_NON_NULL_RET(characterUUID, TAG, "characterUUID is null", NULL);
1631 if (!CALEIsEnableBTAdapter(env))
1633 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1637 // get BluetoothGatt class
1638 OIC_LOG(DEBUG, TAG, "CALEClientGetGattService");
1639 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1640 if (!jni_cid_BluetoothGatt)
1642 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1646 jmethodID jni_mid_getService = (*env)->GetMethodID(
1647 env, jni_cid_BluetoothGatt, "getService",
1648 "(Ljava/util/UUID;)Landroid/bluetooth/BluetoothGattService;");
1649 if (!jni_mid_getService)
1651 OIC_LOG(ERROR, TAG, "jni_mid_getService is null");
1655 jobject jni_obj_service_uuid = CALEClientGetUUIDObject(env, OIC_GATT_SERVICE_UUID);
1656 if (!jni_obj_service_uuid)
1658 OIC_LOG(ERROR, TAG, "jni_obj_service_uuid is null");
1662 // get bluetooth gatt service
1663 OIC_LOG(DEBUG, TAG, "request to get service");
1664 jobject jni_obj_gattService = (*env)->CallObjectMethod(env, bluetoothGatt, jni_mid_getService,
1665 jni_obj_service_uuid);
1666 if (!jni_obj_gattService)
1668 OIC_LOG(ERROR, TAG, "jni_obj_gattService is null");
1672 // get bluetooth gatt service class
1673 jclass jni_cid_BluetoothGattService = (*env)->FindClass(
1674 env, "android/bluetooth/BluetoothGattService");
1675 if (!jni_cid_BluetoothGattService)
1677 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGattService is null");
1681 OIC_LOG(DEBUG, TAG, "get gatt getCharacteristic method");
1682 jmethodID jni_mid_getCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGattService,
1683 "getCharacteristic",
1684 "(Ljava/util/UUID;)"
1685 "Landroid/bluetooth/"
1686 "BluetoothGattCharacteristic;");
1687 if (!jni_mid_getCharacteristic)
1689 OIC_LOG(ERROR, TAG, "jni_mid_getCharacteristic is null");
1693 const char* uuid = (*env)->GetStringUTFChars(env, characterUUID, NULL);
1696 OIC_LOG(ERROR, TAG, "uuid is null");
1700 jobject jni_obj_tx_uuid = CALEClientGetUUIDObject(env, uuid);
1701 if (!jni_obj_tx_uuid)
1703 OIC_LOG(ERROR, TAG, "jni_obj_tx_uuid is null");
1704 (*env)->ReleaseStringUTFChars(env, characterUUID, uuid);
1708 OIC_LOG(DEBUG, TAG, "request to get Characteristic");
1709 jobject jni_obj_GattCharacteristic = (*env)->CallObjectMethod(env, jni_obj_gattService,
1710 jni_mid_getCharacteristic,
1713 (*env)->ReleaseStringUTFChars(env, characterUUID, uuid);
1714 return jni_obj_GattCharacteristic;
1717 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data)
1719 OIC_LOG(DEBUG, TAG, "CALEClientCreateGattCharacteristic");
1720 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
1721 VERIFY_NON_NULL_RET(bluetoothGatt, TAG, "bluetoothGatt is null", NULL);
1722 VERIFY_NON_NULL_RET(data, TAG, "data is null", NULL);
1724 if (!CALEIsEnableBTAdapter(env))
1726 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1730 jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_REQUEST_UUID);
1733 OIC_LOG(ERROR, TAG, "jni_uuid is null");
1737 jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, bluetoothGatt, jni_uuid);
1738 if (!jni_obj_GattCharacteristic)
1740 OIC_LOG(ERROR, TAG, "jni_obj_GattCharacteristic is null");
1744 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth"
1745 "/BluetoothGattCharacteristic");
1746 if (!jni_cid_BTGattCharacteristic)
1748 OIC_LOG(ERROR, TAG, "jni_cid_BTGattCharacteristic is null");
1752 OIC_LOG(DEBUG, TAG, "set value in Characteristic");
1753 jmethodID jni_mid_setValue = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, "setValue",
1755 if (!jni_mid_setValue)
1757 OIC_LOG(ERROR, TAG, "jni_mid_setValue is null");
1761 jboolean ret = (*env)->CallBooleanMethod(env, jni_obj_GattCharacteristic, jni_mid_setValue,
1763 if (JNI_TRUE == ret)
1765 OIC_LOG(DEBUG, TAG, "the locally stored value has been set");
1769 OIC_LOG(ERROR, TAG, "the locally stored value hasn't been set");
1774 jmethodID jni_mid_setWriteType = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic,
1775 "setWriteType", "(I)V");
1776 if (!jni_mid_setWriteType)
1778 OIC_LOG(ERROR, TAG, "jni_mid_setWriteType is null");
1782 jfieldID jni_fid_no_response = (*env)->GetStaticFieldID(env, jni_cid_BTGattCharacteristic,
1783 "WRITE_TYPE_NO_RESPONSE", "I");
1784 if (!jni_fid_no_response)
1786 OIC_LOG(ERROR, TAG, "jni_fid_no_response is not available");
1790 jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTGattCharacteristic,
1791 jni_fid_no_response);
1793 (*env)->CallVoidMethod(env, jni_obj_GattCharacteristic, jni_mid_setWriteType, jni_int_val);
1795 return jni_obj_GattCharacteristic;
1798 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic)
1800 VERIFY_NON_NULL_RET(characteristic, TAG, "characteristic is null", NULL);
1801 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
1803 if (!CALEIsEnableBTAdapter(env))
1805 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1809 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/"
1810 "BluetoothGattCharacteristic");
1811 if (!jni_cid_BTGattCharacteristic)
1813 OIC_LOG(ERROR, TAG, "jni_cid_BTGattCharacteristic is null");
1817 OIC_LOG(DEBUG, TAG, "get value in Characteristic");
1818 jmethodID jni_mid_getValue = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, "getValue",
1820 if (!jni_mid_getValue)
1822 OIC_LOG(ERROR, TAG, "jni_mid_getValue is null");
1826 jbyteArray jni_obj_data_array = (*env)->CallObjectMethod(env, characteristic,
1828 return jni_obj_data_array;
1831 CAResult_t CALEClientCreateUUIDList()
1835 OIC_LOG(ERROR, TAG, "g_jvm is null");
1836 return CA_STATUS_FAILED;
1839 bool isAttached = false;
1841 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1844 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
1845 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1849 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1850 return CA_STATUS_FAILED;
1855 // create new object array
1856 jclass jni_cid_uuid_list = (*env)->FindClass(env, CLASSPATH_BT_UUID);
1857 if (!jni_cid_uuid_list)
1859 OIC_LOG(ERROR, TAG, "jni_cid_uuid_list is null");
1863 jobjectArray jni_obj_uuid_list = (jobjectArray)(*env)->NewObjectArray(env, 1,
1864 jni_cid_uuid_list, NULL);
1865 if (!jni_obj_uuid_list)
1867 OIC_LOG(ERROR, TAG, "jni_obj_uuid_list is null");
1872 jobject jni_obj_uuid = CALEClientGetUUIDObject(env, OIC_GATT_SERVICE_UUID);
1875 OIC_LOG(ERROR, TAG, "jni_obj_uuid is null");
1878 (*env)->SetObjectArrayElement(env, jni_obj_uuid_list, 0, jni_obj_uuid);
1880 g_uuidList = (jobjectArray)(*env)->NewGlobalRef(env, jni_obj_uuid_list);
1884 (*g_jvm)->DetachCurrentThread(g_jvm);
1887 return CA_STATUS_OK;
1894 (*g_jvm)->DetachCurrentThread(g_jvm);
1896 return CA_STATUS_FAILED;
1899 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
1900 jobject characteristic)
1902 VERIFY_NON_NULL(env, TAG, "env is null");
1903 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1904 VERIFY_NON_NULL(characteristic, TAG, "characteristic is null");
1906 if (!CALEIsEnableBTAdapter(env))
1908 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1909 return CA_ADAPTER_NOT_ENABLED;
1912 OIC_LOG(DEBUG, TAG, "CALEClientSetUUIDToDescriptor");
1913 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/"
1914 "BluetoothGattCharacteristic");
1915 if (!jni_cid_BTGattCharacteristic)
1917 OIC_LOG(ERROR, TAG, "jni_cid_BTGattCharacteristic is null");
1918 return CA_STATUS_FAILED;
1921 OIC_LOG(DEBUG, TAG, "set value in Characteristic");
1922 jmethodID jni_mid_getDescriptor = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic,
1924 "(Ljava/util/UUID;)Landroid/bluetooth/"
1925 "BluetoothGattDescriptor;");
1926 if (!jni_mid_getDescriptor)
1928 OIC_LOG(ERROR, TAG, "jni_mid_getDescriptor is null");
1929 return CA_STATUS_FAILED;
1932 jobject jni_obj_cc_uuid = CALEClientGetUUIDObject(env, OIC_GATT_CHARACTERISTIC_CONFIG_UUID);
1933 if (!jni_obj_cc_uuid)
1935 OIC_LOG(ERROR, TAG, "jni_obj_cc_uuid is null");
1936 return CA_STATUS_FAILED;
1939 OIC_LOG(DEBUG, TAG, "request to get descriptor");
1940 jobject jni_obj_descriptor = (*env)->CallObjectMethod(env, characteristic,
1941 jni_mid_getDescriptor, jni_obj_cc_uuid);
1942 if (!jni_obj_descriptor)
1944 OIC_LOG(ERROR, TAG, "jni_obj_descriptor is null");
1945 return CA_STATUS_FAILED;
1948 OIC_LOG(DEBUG, TAG, "set value in descriptor");
1949 jclass jni_cid_descriptor = (*env)->FindClass(env,
1950 "android/bluetooth/BluetoothGattDescriptor");
1951 if (!jni_cid_descriptor)
1953 OIC_LOG(ERROR, TAG, "jni_cid_descriptor is null");
1954 return CA_STATUS_FAILED;
1957 jmethodID jni_mid_setValue = (*env)->GetMethodID(env, jni_cid_descriptor, "setValue", "([B)Z");
1958 if (!jni_mid_setValue)
1960 OIC_LOG(ERROR, TAG, "jni_mid_setValue is null");
1961 return CA_STATUS_FAILED;
1964 jfieldID jni_fid_NotiValue = (*env)->GetStaticFieldID(env, jni_cid_descriptor,
1965 "ENABLE_NOTIFICATION_VALUE", "[B");
1966 if (!jni_fid_NotiValue)
1968 OIC_LOG(ERROR, TAG, "jni_fid_NotiValue is null");
1969 return CA_STATUS_FAILED;
1972 OIC_LOG(DEBUG, TAG, "get ENABLE_NOTIFICATION_VALUE");
1974 jboolean jni_setvalue = (*env)->CallBooleanMethod(
1975 env, jni_obj_descriptor, jni_mid_setValue,
1976 (jbyteArray)(*env)->GetStaticObjectField(env, jni_cid_descriptor, jni_fid_NotiValue));
1979 OIC_LOG(DEBUG, TAG, "setValue success");
1983 OIC_LOG(ERROR, TAG, "setValue has failed");
1984 return CA_STATUS_FAILED;
1987 jclass jni_cid_gatt = (*env)->FindClass(env, "android/bluetooth/BluetoothGatt");
1990 OIC_LOG(ERROR, TAG, "jni_cid_gatt is null");
1991 return CA_STATUS_FAILED;
1994 OIC_LOG(DEBUG, TAG, "write Descriptor in gatt object");
1995 jmethodID jni_mid_writeDescriptor = (*env)->GetMethodID(env, jni_cid_gatt, "writeDescriptor",
1996 "(Landroid/bluetooth/"
1997 "BluetoothGattDescriptor;)Z");
1998 if (!jni_mid_writeDescriptor)
2000 OIC_LOG(ERROR, TAG, "jni_mid_writeDescriptor is null");
2001 return CA_STATUS_FAILED;
2004 OIC_LOG(DEBUG, TAG, "request to write descriptor");
2005 jboolean jni_ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_writeDescriptor,
2006 jni_obj_descriptor);
2009 OIC_LOG(DEBUG, TAG, "writeDescriptor success");
2013 OIC_LOG(ERROR, TAG, "writeDescriptor has failed");
2014 return CA_STATUS_FAILED;
2017 return CA_STATUS_OK;
2020 void CALEClientCreateScanDeviceList(JNIEnv *env)
2022 OIC_LOG(DEBUG, TAG, "CALEClientCreateScanDeviceList");
2023 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
2025 ca_mutex_lock(g_deviceListMutex);
2026 // create new object array
2027 if (g_deviceList == NULL)
2029 OIC_LOG(DEBUG, TAG, "Create device list");
2031 g_deviceList = u_arraylist_create();
2033 ca_mutex_unlock(g_deviceListMutex);
2036 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device)
2038 OIC_LOG(DEBUG, TAG, "IN - CALEClientAddScanDeviceToList");
2039 VERIFY_NON_NULL(device, TAG, "device is null");
2040 VERIFY_NON_NULL(env, TAG, "env is null");
2042 ca_mutex_lock(g_deviceListMutex);
2046 OIC_LOG(ERROR, TAG, "gdevice_list is null");
2047 ca_mutex_unlock(g_deviceListMutex);
2048 return CA_STATUS_FAILED;
2051 jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
2052 if (!jni_remoteAddress)
2054 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
2055 ca_mutex_unlock(g_deviceListMutex);
2056 return CA_STATUS_FAILED;
2059 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2062 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2063 ca_mutex_unlock(g_deviceListMutex);
2064 return CA_STATUS_FAILED;
2067 if (!CALEClientIsDeviceInScanDeviceList(env, remoteAddress))
2069 jobject gdevice = (*env)->NewGlobalRef(env, device);
2070 u_arraylist_add(g_deviceList, gdevice);
2071 OIC_LOG(DEBUG, TAG, "Set Object to Array as Element");
2073 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2075 ca_mutex_unlock(g_deviceListMutex);
2077 OIC_LOG(DEBUG, TAG, "OUT - CALEClientAddScanDeviceToList");
2078 return CA_STATUS_OK;
2081 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char* remoteAddress)
2083 OIC_LOG(DEBUG, TAG, "IN - CALEClientIsDeviceInScanDeviceList");
2084 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
2085 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", true);
2089 OIC_LOG(DEBUG, TAG, "g_deviceList is null");
2093 uint32_t length = u_arraylist_length(g_deviceList);
2094 for (uint32_t index = 0; index < length; index++)
2096 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
2099 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2103 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
2104 if (!jni_setAddress)
2106 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2110 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2113 OIC_LOG(ERROR, TAG, "setAddress is null");
2117 if (!strcmp(remoteAddress, setAddress))
2119 OIC_LOG(DEBUG, TAG, "the device is already set");
2120 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2124 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2127 OIC_LOG(DEBUG, TAG, "OUT - CALEClientIsDeviceInScanDeviceList");
2128 OIC_LOG(DEBUG, TAG, "there are no the device in list. we can add");
2133 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env)
2135 OIC_LOG(DEBUG, TAG, "CALEClientRemoveAllScanDevices");
2136 VERIFY_NON_NULL(env, TAG, "env is null");
2138 ca_mutex_lock(g_deviceListMutex);
2142 OIC_LOG(ERROR, TAG, "g_deviceList is null");
2143 ca_mutex_unlock(g_deviceListMutex);
2144 return CA_STATUS_FAILED;
2147 uint32_t length = u_arraylist_length(g_deviceList);
2148 for (uint32_t index = 0; index < length; index++)
2150 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
2153 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2156 (*env)->DeleteGlobalRef(env, jarrayObj);
2159 OICFree(g_deviceList);
2160 g_deviceList = NULL;
2162 ca_mutex_unlock(g_deviceListMutex);
2163 return CA_STATUS_OK;
2166 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring address)
2168 OIC_LOG(DEBUG, TAG, "CALEClientRemoveDeviceInScanDeviceList");
2169 VERIFY_NON_NULL(address, TAG, "address is null");
2170 VERIFY_NON_NULL(env, TAG, "env is null");
2172 ca_mutex_lock(g_deviceListMutex);
2176 OIC_LOG(ERROR, TAG, "g_deviceList is null");
2177 ca_mutex_unlock(g_deviceListMutex);
2178 return CA_STATUS_FAILED;
2181 uint32_t length = u_arraylist_length(g_deviceList);
2182 for (uint32_t index = 0; index < length; index++)
2184 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
2187 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2188 ca_mutex_unlock(g_deviceListMutex);
2189 return CA_STATUS_FAILED;
2192 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
2193 if (!jni_setAddress)
2195 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2196 ca_mutex_unlock(g_deviceListMutex);
2197 return CA_STATUS_FAILED;
2200 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2203 OIC_LOG(ERROR, TAG, "setAddress is null");
2204 ca_mutex_unlock(g_deviceListMutex);
2205 return CA_STATUS_FAILED;
2208 const char* remoteAddress = (*env)->GetStringUTFChars(env, address, NULL);
2211 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2212 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2213 ca_mutex_unlock(g_deviceListMutex);
2214 return CA_STATUS_FAILED;
2217 if (!strcmp(setAddress, remoteAddress))
2219 OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
2220 (*env)->DeleteGlobalRef(env, jarrayObj);
2221 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2222 (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
2224 if (NULL == u_arraylist_remove(g_deviceList, index))
2226 OIC_LOG(ERROR, TAG, "List removal failed.");
2227 ca_mutex_unlock(g_deviceListMutex);
2228 return CA_STATUS_FAILED;
2230 ca_mutex_unlock(g_deviceListMutex);
2231 return CA_STATUS_OK;
2233 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2234 (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
2237 ca_mutex_unlock(g_deviceListMutex);
2238 OIC_LOG(DEBUG, TAG, "There are no object in the device list");
2240 return CA_STATUS_OK;
2247 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt)
2249 OIC_LOG(DEBUG, TAG, "CALEClientAddGattobjToList");
2250 VERIFY_NON_NULL(env, TAG, "env is null");
2251 VERIFY_NON_NULL(gatt, TAG, "gatt is null");
2253 ca_mutex_lock(g_gattObjectMutex);
2255 jstring jni_remoteAddress = CALEClientGetAddressFromGattObj(env, gatt);
2256 if (!jni_remoteAddress)
2258 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
2259 ca_mutex_unlock(g_gattObjectMutex);
2260 return CA_STATUS_FAILED;
2263 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2266 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2267 ca_mutex_unlock(g_gattObjectMutex);
2268 return CA_STATUS_FAILED;
2271 if (!CALEClientIsGattObjInList(env, remoteAddress))
2273 jobject newGatt = (*env)->NewGlobalRef(env, gatt);
2274 u_arraylist_add(g_gattObjectList, newGatt);
2275 OIC_LOG(DEBUG, TAG, "Set GATT Object to Array as Element");
2278 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2279 ca_mutex_unlock(g_gattObjectMutex);
2280 return CA_STATUS_OK;
2283 bool CALEClientIsGattObjInList(JNIEnv *env, const char* remoteAddress)
2285 OIC_LOG(DEBUG, TAG, "CALEClientIsGattObjInList");
2286 VERIFY_NON_NULL(env, TAG, "env is null");
2287 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", true);
2289 uint32_t length = u_arraylist_length(g_gattObjectList);
2290 for (uint32_t index = 0; index < length; index++)
2293 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2296 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2300 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2301 if (!jni_setAddress)
2303 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2307 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2310 OIC_LOG(ERROR, TAG, "setAddress is null");
2314 if (!strcmp(remoteAddress, setAddress))
2316 OIC_LOG(DEBUG, TAG, "the device is already set");
2317 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2322 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2327 OIC_LOG(DEBUG, TAG, "There are no GATT object in list. it can be added");
2331 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress)
2333 OIC_LOG(DEBUG, TAG, "CALEClientGetGattObjInList");
2334 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
2335 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", NULL);
2337 ca_mutex_lock(g_gattObjectMutex);
2338 uint32_t length = u_arraylist_length(g_gattObjectList);
2339 for (uint32_t index = 0; index < length; index++)
2341 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2344 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2345 ca_mutex_unlock(g_gattObjectMutex);
2349 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2350 if (!jni_setAddress)
2352 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2353 ca_mutex_unlock(g_gattObjectMutex);
2357 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2360 OIC_LOG(ERROR, TAG, "setAddress is null");
2361 ca_mutex_unlock(g_gattObjectMutex);
2365 if (!strcmp(remoteAddress, setAddress))
2367 OIC_LOG(DEBUG, TAG, "the device is already set");
2368 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2369 ca_mutex_unlock(g_gattObjectMutex);
2372 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2375 ca_mutex_unlock(g_gattObjectMutex);
2376 OIC_LOG(DEBUG, TAG, "There are no the gatt object in list");
2380 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env)
2382 OIC_LOG(DEBUG, TAG, "CALEClientRemoveAllGattObjs");
2383 VERIFY_NON_NULL(env, TAG, "env is null");
2385 ca_mutex_lock(g_gattObjectMutex);
2386 if (!g_gattObjectList)
2388 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
2389 ca_mutex_unlock(g_gattObjectMutex);
2390 return CA_STATUS_FAILED;
2393 uint32_t length = u_arraylist_length(g_gattObjectList);
2394 for (uint32_t index = 0; index < length; index++)
2396 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2399 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2402 (*env)->DeleteGlobalRef(env, jarrayObj);
2405 OICFree(g_gattObjectList);
2406 g_gattObjectList = NULL;
2407 ca_mutex_unlock(g_gattObjectMutex);
2408 return CA_STATUS_OK;
2411 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt)
2413 OIC_LOG(DEBUG, TAG, "CALEClientRemoveGattObj");
2414 VERIFY_NON_NULL(gatt, TAG, "gatt is null");
2415 VERIFY_NON_NULL(env, TAG, "env is null");
2417 ca_mutex_lock(g_gattObjectMutex);
2418 if (!g_gattObjectList)
2420 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
2421 ca_mutex_unlock(g_gattObjectMutex);
2422 return CA_STATUS_FAILED;
2425 uint32_t length = u_arraylist_length(g_gattObjectList);
2426 for (uint32_t index = 0; index < length; index++)
2428 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2431 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2432 ca_mutex_unlock(g_gattObjectMutex);
2433 return CA_STATUS_FAILED;
2436 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2437 if (!jni_setAddress)
2439 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2440 ca_mutex_unlock(g_gattObjectMutex);
2441 return CA_STATUS_FAILED;
2444 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2447 OIC_LOG(ERROR, TAG, "setAddress is null");
2448 ca_mutex_unlock(g_gattObjectMutex);
2449 return CA_STATUS_FAILED;
2452 jstring jni_remoteAddress = CALEClientGetAddressFromGattObj(env, gatt);
2453 if (!jni_remoteAddress)
2455 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
2456 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2457 ca_mutex_unlock(g_gattObjectMutex);
2458 return CA_STATUS_FAILED;
2461 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2464 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2465 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2466 ca_mutex_unlock(g_gattObjectMutex);
2467 return CA_STATUS_FAILED;
2470 if (!strcmp(setAddress, remoteAddress))
2472 OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
2473 (*env)->DeleteGlobalRef(env, jarrayObj);
2474 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2475 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2477 if (NULL == u_arraylist_remove(g_gattObjectList, index))
2479 OIC_LOG(ERROR, TAG, "List removal failed.");
2480 ca_mutex_unlock(g_gattObjectMutex);
2481 return CA_STATUS_FAILED;
2483 ca_mutex_unlock(g_gattObjectMutex);
2484 return CA_STATUS_OK;
2486 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2487 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2490 ca_mutex_unlock(g_gattObjectMutex);
2491 OIC_LOG(DEBUG, TAG, "there are no target object");
2492 return CA_STATUS_OK;
2495 CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr)
2497 OIC_LOG(DEBUG, TAG, "CALEClientRemoveGattObjForAddr");
2498 VERIFY_NON_NULL(addr, TAG, "addr is null");
2499 VERIFY_NON_NULL(env, TAG, "env is null");
2501 ca_mutex_lock(g_gattObjectMutex);
2502 if (!g_gattObjectList)
2504 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
2505 ca_mutex_unlock(g_gattObjectMutex);
2506 return CA_STATUS_FAILED;
2509 uint32_t length = u_arraylist_length(g_gattObjectList);
2510 for (uint32_t index = 0; index < length; index++)
2512 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2515 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2516 ca_mutex_unlock(g_gattObjectMutex);
2517 return CA_STATUS_FAILED;
2520 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2521 if (!jni_setAddress)
2523 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2524 ca_mutex_unlock(g_gattObjectMutex);
2525 return CA_STATUS_FAILED;
2528 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2531 OIC_LOG(ERROR, TAG, "setAddress is null");
2532 ca_mutex_unlock(g_gattObjectMutex);
2533 return CA_STATUS_FAILED;
2536 const char* remoteAddress = (*env)->GetStringUTFChars(env, addr, NULL);
2539 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2540 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2541 ca_mutex_unlock(g_gattObjectMutex);
2542 return CA_STATUS_FAILED;
2545 if (!strcmp(setAddress, remoteAddress))
2547 OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
2548 (*env)->DeleteGlobalRef(env, jarrayObj);
2550 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2551 (*env)->ReleaseStringUTFChars(env, addr, remoteAddress);
2552 if (NULL == u_arraylist_remove(g_gattObjectList, index))
2554 OIC_LOG(ERROR, TAG, "List removal failed.");
2555 ca_mutex_unlock(g_gattObjectMutex);
2556 return CA_STATUS_FAILED;
2558 ca_mutex_unlock(g_gattObjectMutex);
2559 return CA_STATUS_OK;
2561 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2562 (*env)->ReleaseStringUTFChars(env, addr, remoteAddress);
2565 ca_mutex_unlock(g_gattObjectMutex);
2566 OIC_LOG(DEBUG, TAG, "there are no target object");
2567 return CA_STATUS_FAILED;
2574 CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
2575 uint16_t notificationState, uint16_t sendState)
2577 VERIFY_NON_NULL(address, TAG, "address is null");
2579 CALEState_t *newstate = (CALEState_t*) OICMalloc(sizeof(CALEState_t));
2582 OIC_LOG(ERROR, TAG, "out of memory");
2583 return CA_MEMORY_ALLOC_FAILED;
2586 if (strlen(address) > CA_MACADDR_SIZE)
2588 OIC_LOG(ERROR, TAG, "address is not proper");
2590 return CA_STATUS_FAILED;
2593 OICStrcpy(newstate->address, sizeof(newstate->address), address);
2594 newstate->connectedState = connectedState;
2595 newstate->notificationState = notificationState;
2596 newstate->sendState = sendState;
2597 return CALEClientAddDeviceStateToList(newstate);
2600 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state)
2602 VERIFY_NON_NULL(state, TAG, "state is null");
2604 ca_mutex_lock(g_deviceStateListMutex);
2606 if (!g_deviceStateList)
2608 OIC_LOG(ERROR, TAG, "gdevice_list is null");
2609 ca_mutex_unlock(g_deviceStateListMutex);
2610 return CA_STATUS_FAILED;
2613 if (CALEClientIsDeviceInList(state->address))
2615 CALEState_t* curState = CALEClientGetStateInfo(state->address);
2618 OIC_LOG(ERROR, TAG, "curState is null");
2619 ca_mutex_unlock(g_deviceStateListMutex);
2620 return CA_STATUS_FAILED;
2623 if (STATE_CHARACTER_NO_CHANGE == state->notificationState)
2625 state->notificationState = curState->notificationState;
2628 // delete previous state for update new state
2629 CAResult_t res = CALEClientRemoveDeviceState(state->address);
2630 if (CA_STATUS_OK != res)
2632 OIC_LOG(ERROR, TAG, "CALEClientRemoveDeviceState has failed");
2633 ca_mutex_unlock(g_deviceStateListMutex);
2637 u_arraylist_add(g_deviceStateList, state); // update new state
2638 OIC_LOG_V(DEBUG, TAG, "Set State Info to List : %d, %d",
2639 state->connectedState, state->notificationState);
2641 ca_mutex_unlock(g_deviceStateListMutex);
2642 return CA_STATUS_OK;
2645 bool CALEClientIsDeviceInList(const char* remoteAddress)
2647 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
2649 if (!g_deviceStateList)
2651 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2655 uint32_t length = u_arraylist_length(g_deviceStateList);
2656 for (uint32_t index = 0; index < length; index++)
2658 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2661 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2665 if (!strcmp(remoteAddress, state->address))
2667 OIC_LOG(DEBUG, TAG, "the device is already set");
2676 OIC_LOG(DEBUG, TAG, "there are no the device in list.");
2680 CAResult_t CALEClientRemoveAllDeviceState()
2682 OIC_LOG(DEBUG, TAG, "CALENativeRemoveAllDevices");
2684 ca_mutex_lock(g_deviceStateListMutex);
2685 if (!g_deviceStateList)
2687 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2688 ca_mutex_unlock(g_deviceStateListMutex);
2689 return CA_STATUS_FAILED;
2692 uint32_t length = u_arraylist_length(g_deviceStateList);
2693 for (uint32_t index = 0; index < length; index++)
2695 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2698 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2704 OICFree(g_deviceStateList);
2705 g_deviceStateList = NULL;
2706 ca_mutex_unlock(g_deviceStateListMutex);
2708 return CA_STATUS_OK;
2711 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress)
2713 OIC_LOG(DEBUG, TAG, "CALEClientRemoveDeviceState");
2714 VERIFY_NON_NULL(remoteAddress, TAG, "remoteAddress is null");
2716 if (!g_deviceStateList)
2718 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2719 return CA_STATUS_FAILED;
2722 uint32_t length = u_arraylist_length(g_deviceStateList);
2723 for (uint32_t index = 0; index < length; index++)
2725 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2728 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2732 if (!strcmp(state->address, remoteAddress))
2734 OIC_LOG_V(DEBUG, TAG, "remove state : %s", remoteAddress);
2737 if (NULL == u_arraylist_remove(g_deviceStateList, index))
2739 OIC_LOG(ERROR, TAG, "List removal failed.");
2740 return CA_STATUS_FAILED;
2743 return CA_STATUS_OK;
2747 return CA_STATUS_FAILED;
2750 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress)
2752 OIC_LOG(DEBUG, TAG, "CALEClientGetStateInfo");
2753 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", NULL);
2755 if (!g_deviceStateList)
2757 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2761 uint32_t length = u_arraylist_length(g_deviceStateList);
2762 for (uint32_t index = 0; index < length; index++)
2764 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2767 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2771 if (!strcmp(state->address, remoteAddress))
2773 OIC_LOG_V(DEBUG, TAG, "get state : %s", remoteAddress);
2780 bool CALEClientIsConnectedDevice(const char* remoteAddress)
2782 OIC_LOG(DEBUG, TAG, "CALEClientIsConnectedDevice");
2783 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
2785 ca_mutex_lock(g_deviceStateListMutex);
2786 if (!g_deviceStateList)
2788 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2789 ca_mutex_unlock(g_deviceStateListMutex);
2793 uint32_t length = u_arraylist_length(g_deviceStateList);
2794 for (uint32_t index = 0; index < length; index++)
2796 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2799 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2803 if (!strcmp(state->address, remoteAddress))
2805 OIC_LOG(DEBUG, TAG, "check whether it is connected or not");
2807 if (STATE_CONNECTED == state->connectedState)
2809 ca_mutex_unlock(g_deviceStateListMutex);
2814 ca_mutex_unlock(g_deviceStateListMutex);
2819 ca_mutex_unlock(g_deviceStateListMutex);
2823 bool CALEClientIsSetCharacteristic(const char* remoteAddress)
2825 OIC_LOG(DEBUG, TAG, "CALEClientIsSetCharacteristic");
2826 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
2828 ca_mutex_lock(g_deviceStateListMutex);
2829 if (!g_deviceStateList)
2831 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2832 ca_mutex_unlock(g_deviceStateListMutex);
2836 uint32_t length = u_arraylist_length(g_deviceStateList);
2837 for (uint32_t index = 0; index < length; index++)
2839 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2842 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2846 if (!strcmp(state->address, remoteAddress))
2848 OIC_LOG_V(DEBUG, TAG, "check whether it was set or not:%d", state->notificationState);
2850 if (STATE_CHARACTER_SET == state->notificationState)
2852 ca_mutex_unlock(g_deviceStateListMutex);
2857 ca_mutex_unlock(g_deviceStateListMutex);
2863 ca_mutex_unlock(g_deviceStateListMutex);
2867 void CALEClientCreateDeviceList()
2869 OIC_LOG(DEBUG, TAG, "CALEClientCreateDeviceList");
2871 // create new object array
2872 if (!g_gattObjectList)
2874 OIC_LOG(DEBUG, TAG, "Create g_gattObjectList");
2876 g_gattObjectList = u_arraylist_create();
2879 if (!g_deviceStateList)
2881 OIC_LOG(DEBUG, TAG, "Create g_deviceStateList");
2883 g_deviceStateList = u_arraylist_create();
2888 OIC_LOG(DEBUG, TAG, "Create g_deviceList");
2890 g_deviceList = u_arraylist_create();
2895 * Check Sent Count for remove g_sendBuffer
2897 void CALEClientUpdateSendCnt(JNIEnv *env)
2899 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
2901 ca_mutex_lock(g_threadMutex);
2905 if (g_targetCnt <= g_currentSentCnt)
2908 g_currentSentCnt = 0;
2912 (*env)->DeleteGlobalRef(env, g_sendBuffer);
2913 g_sendBuffer = NULL;
2915 // notity the thread
2916 ca_cond_signal(g_threadCond);
2917 CALEClientSetSendFinishFlag(true);
2918 OIC_LOG(DEBUG, TAG, "set signal for send data");
2921 ca_mutex_unlock(g_threadMutex);
2924 CAResult_t CALEClientInitGattMutexVaraibles()
2926 OIC_LOG(DEBUG, TAG, "IN");
2928 if (NULL == g_bleReqRespClientCbMutex)
2930 g_bleReqRespClientCbMutex = ca_mutex_new();
2931 if (NULL == g_bleReqRespClientCbMutex)
2933 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2934 return CA_STATUS_FAILED;
2938 if (NULL == g_bleServerBDAddressMutex)
2940 g_bleServerBDAddressMutex = ca_mutex_new();
2941 if (NULL == g_bleServerBDAddressMutex)
2943 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2944 return CA_STATUS_FAILED;
2948 if (NULL == g_threadMutex)
2950 g_threadMutex = ca_mutex_new();
2951 if (NULL == g_threadMutex)
2953 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2954 return CA_STATUS_FAILED;
2958 if (NULL == g_threadSendMutex)
2960 g_threadSendMutex = ca_mutex_new();
2961 if (NULL == g_threadSendMutex)
2963 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2964 return CA_STATUS_FAILED;
2968 if (NULL == g_deviceListMutex)
2970 g_deviceListMutex = ca_mutex_new();
2971 if (NULL == g_deviceListMutex)
2973 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2974 return CA_STATUS_FAILED;
2978 if (NULL == g_gattObjectMutex)
2980 g_gattObjectMutex = ca_mutex_new();
2981 if (NULL == g_gattObjectMutex)
2983 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2984 return CA_STATUS_FAILED;
2988 if (NULL == g_deviceStateListMutex)
2990 g_deviceStateListMutex = ca_mutex_new();
2991 if (NULL == g_deviceStateListMutex)
2993 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2994 return CA_STATUS_FAILED;
2998 if (NULL == g_SendFinishMutex)
3000 g_SendFinishMutex = ca_mutex_new();
3001 if (NULL == g_SendFinishMutex)
3003 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3004 return CA_STATUS_FAILED;
3008 OIC_LOG(DEBUG, TAG, "OUT");
3009 return CA_STATUS_OK;
3012 void CALEClientTerminateGattMutexVariables()
3014 OIC_LOG(DEBUG, TAG, "IN");
3016 ca_mutex_free(g_bleReqRespClientCbMutex);
3017 g_bleReqRespClientCbMutex = NULL;
3019 ca_mutex_free(g_bleServerBDAddressMutex);
3020 g_bleServerBDAddressMutex = NULL;
3022 ca_mutex_free(g_threadMutex);
3023 g_threadMutex = NULL;
3025 ca_mutex_free(g_threadSendMutex);
3026 g_threadSendMutex = NULL;
3028 ca_mutex_free(g_deviceListMutex);
3029 g_deviceListMutex = NULL;
3031 ca_mutex_free(g_SendFinishMutex);
3032 g_SendFinishMutex = NULL;
3034 OIC_LOG(DEBUG, TAG, "OUT");
3037 void CALEClientSetSendFinishFlag(bool flag)
3039 OIC_LOG_V(DEBUG, TAG, "g_isFinishedSendData is %d", flag);
3041 ca_mutex_lock(g_SendFinishMutex);
3042 g_isFinishedSendData = flag;
3043 ca_mutex_unlock(g_SendFinishMutex);
3050 CAResult_t CAStartLEGattClient()
3052 CAResult_t res = CALEClientStartMulticastServer();
3053 if (CA_STATUS_OK != res)
3055 OIC_LOG(ERROR, TAG, "CALEClientStartMulticastServer has failed");
3059 g_isStartedLEClient = true;
3065 void CAStopLEGattClient()
3067 OIC_LOG(DEBUG, TAG, "CAStopBLEGattClient");
3071 OIC_LOG(ERROR, TAG, "g_jvm is null");
3075 bool isAttached = false;
3077 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
3080 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
3081 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
3085 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
3091 CAResult_t ret = CALEClientDisconnectAll(env);
3092 if (CA_STATUS_OK != ret)
3094 OIC_LOG(ERROR, TAG, "CALEClientDisconnectAll has failed");
3097 ret = CALEClientStopScan();
3098 if(CA_STATUS_OK != ret)
3100 OIC_LOG(ERROR, TAG, "CALEClientStopScan has failed");
3103 ca_cond_signal(g_threadCond);
3107 (*g_jvm)->DetachCurrentThread(g_jvm);
3112 void CATerminateLEGattClient()
3114 OIC_LOG(DEBUG, TAG, "Terminate GATT Client");
3115 CALEClientTerminate();
3118 CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress, const uint8_t *data,
3119 uint32_t dataLen, CALETransferType_t type,
3122 OIC_LOG(DEBUG, TAG, "call CALEClientSendUnicastMessage");
3123 VERIFY_NON_NULL(data, TAG, "data is null");
3124 VERIFY_NON_NULL(remoteAddress, TAG, "remoteAddress is null");
3126 if (LE_UNICAST != type || position < 0)
3128 OIC_LOG(ERROR, TAG, "this request is not unicast");
3129 return CA_STATUS_INVALID_PARAM;
3132 return CALEClientSendUnicastMessage(remoteAddress, data, dataLen);
3135 CAResult_t CAUpdateCharacteristicsToAllGattServers(const uint8_t *data, uint32_t dataLen)
3137 OIC_LOG(DEBUG, TAG, "call CALEClientSendMulticastMessage");
3138 VERIFY_NON_NULL(data, TAG, "data is null");
3140 return CALEClientSendMulticastMessage(data, dataLen);
3143 void CASetLEReqRespClientCallback(CABLEDataReceivedCallback callback)
3145 OIC_LOG(DEBUG, TAG, "IN");
3147 ca_mutex_lock(g_bleReqRespClientCbMutex);
3148 g_CABLEClientDataReceivedCallback = callback;
3149 ca_mutex_unlock(g_bleReqRespClientCbMutex);
3151 OIC_LOG(DEBUG, TAG, "OUT");
3154 void CASetLEClientThreadPoolHandle(ca_thread_pool_t handle)
3156 OIC_LOG(DEBUG, TAG, "IN");
3158 CALEClientInitialize(handle);
3160 OIC_LOG(DEBUG, TAG, "OUT");
3163 CAResult_t CAGetLEAddress(char **local_address)
3165 VERIFY_NON_NULL(local_address, TAG, "local_address");
3166 OIC_LOG(INFO, TAG, "CAGetLEAddress is not support");
3167 return CA_NOT_SUPPORTED;
3170 JNIEXPORT void JNICALL
3171 Java_org_iotivity_ca_CaLeClientInterface_caLeRegisterLeScanCallback(JNIEnv *env, jobject obj,
3174 OIC_LOG(DEBUG, TAG, "CaLeRegisterLeScanCallback");
3175 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3176 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3177 VERIFY_NON_NULL_VOID(callback, TAG, "callback is null");
3179 g_leScanCallback = (*env)->NewGlobalRef(env, callback);
3182 JNIEXPORT void JNICALL
3183 Java_org_iotivity_ca_CaLeClientInterface_caLeRegisterGattCallback(JNIEnv *env, jobject obj,
3186 OIC_LOG(DEBUG, TAG, "CaLeRegisterGattCallback");
3187 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3188 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3189 VERIFY_NON_NULL_VOID(callback, TAG, "callback is null");
3191 g_leGattCallback = (*env)->NewGlobalRef(env, callback);
3194 JNIEXPORT void JNICALL
3195 Java_org_iotivity_ca_CaLeClientInterface_caLeScanCallback(JNIEnv *env, jobject obj,
3198 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3199 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3200 VERIFY_NON_NULL_VOID(device, TAG, "device is null");
3202 CAResult_t res = CALEClientAddScanDeviceToList(env, device);
3203 if (CA_STATUS_OK != res)
3205 OIC_LOG_V(ERROR, TAG, "CALEClientAddScanDeviceToList has failed : %d", res);
3210 * Class: org_iotivity_ca_jar_caleinterface
3211 * Method: CALeGattConnectionStateChangeCallback
3212 * Signature: (Landroid/bluetooth/BluetoothGatt;II)V
3214 JNIEXPORT void JNICALL
3215 Java_org_iotivity_ca_CaLeClientInterface_caLeGattConnectionStateChangeCallback(JNIEnv *env,
3221 OIC_LOG_V(DEBUG, TAG, "CALeGattConnectionStateChangeCallback - status %d, newstate %d", status,
3223 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3224 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3225 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3227 if (GATT_SUCCESS == status && STATE_CONNECTED == newstate) // le connected
3229 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3235 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3238 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED,
3239 STATE_CHARACTER_NO_CHANGE,
3241 if (CA_STATUS_OK != res)
3243 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3244 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3247 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3250 CAResult_t res = CALEClientAddGattobjToList(env, gatt);
3251 if (CA_STATUS_OK != res)
3253 OIC_LOG(ERROR, TAG, "CALEClientAddGattobjToList has failed");
3257 res = CALEClientDiscoverServices(env, gatt);
3258 if (CA_STATUS_OK != res)
3260 OIC_LOG(ERROR, TAG, "CALEClientDiscoverServices has failed");
3264 else if (GATT_SUCCESS == status && STATE_DISCONNECTED == newstate) // le disconnected
3266 CAResult_t res = CALEClientStartScan();
3267 if (CA_STATUS_OK != res)
3269 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
3272 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3275 OIC_LOG(ERROR, TAG, "CALEClientGetAddressFromGattObj has failed");
3278 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3281 res = CALEClientUpdateDeviceState(address, STATE_DISCONNECTED,
3282 STATE_CHARACTER_NO_CHANGE,
3284 if (CA_STATUS_OK != res)
3286 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3288 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3291 res = CALEClientGattClose(env, gatt);
3292 if (CA_STATUS_OK != res)
3294 OIC_LOG(ERROR, TAG, "CALEClientGattClose has failed");
3300 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3303 OIC_LOG(ERROR, TAG, "jni_address is null");
3308 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3311 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_DISCONNECTED,
3312 STATE_CHARACTER_NO_CHANGE,
3314 if (CA_STATUS_OK != res)
3316 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3319 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3321 CAResult_t res = CALEClientGattClose(env, gatt);
3322 if (CA_STATUS_OK != res)
3324 OIC_LOG(ERROR, TAG, "CALEClientGattClose has failed");
3334 CALEClientSendFinish(env, gatt);
3339 * Class: org_iotivity_ca_jar_caleinterface
3340 * Method: CALeGattServicesDiscoveredCallback
3341 * Signature: (Landroid/bluetooth/BluetoothGatt;I)V
3343 JNIEXPORT void JNICALL
3344 Java_org_iotivity_ca_CaLeClientInterface_caLeGattServicesDiscoveredCallback(JNIEnv *env,
3349 OIC_LOG_V(DEBUG, TAG, "CALeGattServicesDiscoveredCallback - status %d: ", status);
3350 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3351 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3352 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3354 if (0 != status) // discovery error
3356 CALEClientSendFinish(env, gatt);
3360 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3363 CALEClientSendFinish(env, gatt);
3367 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3370 CALEClientSendFinish(env, gatt);
3374 if (!CALEClientIsSetCharacteristic(address))
3376 jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_RESPONSE_UUID);
3379 OIC_LOG(ERROR, TAG, "jni_uuid is null");
3383 jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, gatt, jni_uuid);
3384 if (!jni_obj_GattCharacteristic)
3386 OIC_LOG(ERROR, TAG, "jni_obj_GattCharacteristic is null");
3390 CAResult_t res = CALEClientSetCharacteristicNotification(env, gatt,
3391 jni_obj_GattCharacteristic);
3392 if (CA_STATUS_OK != res)
3394 OIC_LOG(ERROR, TAG, "CALEClientSetCharacteristicNotification has failed");
3398 res = CALEClientSetUUIDToDescriptor(env, gatt, jni_obj_GattCharacteristic);
3399 if (CA_STATUS_OK != res)
3401 OIC_LOG(INFO, TAG, "Descriptor of the uuid is not found");
3402 CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
3403 if (CA_STATUS_OK != res)
3405 OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
3410 res = CALEClientUpdateDeviceState(address, STATE_CONNECTED, STATE_CHARACTER_SET,
3412 if (CA_STATUS_OK != res)
3414 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3420 CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
3421 if (CA_STATUS_OK != res)
3423 OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
3427 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3432 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3433 CALEClientSendFinish(env, gatt);
3438 * Class: org_iotivity_ca_jar_caleinterface
3439 * Method: CALeGattCharacteristicWritjclasseCallback
3440 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;I)V
3442 JNIEXPORT void JNICALL
3443 Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicWriteCallback(
3444 JNIEnv *env, jobject obj, jobject gatt, jbyteArray data,
3447 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - status : %d", status);
3448 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3449 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3450 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3453 char* wroteData = (char*) (*env)->GetByteArrayElements(env, data, &isCopy);
3455 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - write data : %s", wroteData);
3457 // send success & signal
3458 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3464 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3470 if (GATT_SUCCESS != status) // error case
3472 OIC_LOG(ERROR, TAG, "send failure");
3473 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED, STATE_CHARACTER_SET,
3475 if (CA_STATUS_OK != res)
3477 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3479 CALEClientSendFinish(env, gatt);
3483 OIC_LOG(DEBUG, TAG, "send success");
3484 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED, STATE_CHARACTER_SET,
3485 STATE_SEND_SUCCESS);
3486 if (CA_STATUS_OK != res)
3488 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3490 CALEClientUpdateSendCnt(env);
3493 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3499 CALEClientSendFinish(env, gatt);
3504 * Class: org_iotivity_ca_jar_caleinterface
3505 * Method: CALeGattCharacteristicChangedCallback
3506 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;)V
3508 JNIEXPORT void JNICALL
3509 Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicChangedCallback(
3510 JNIEnv *env, jobject obj, jobject gatt, jbyteArray data)
3512 OIC_LOG(DEBUG, TAG, "CALeGattCharacteristicChangedCallback");
3513 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3514 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3515 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3516 VERIFY_NON_NULL_VOID(data, TAG, "data is null");
3518 // get Byte Array and convert to uint8_t*
3519 jint length = (*env)->GetArrayLength(env, data);
3522 jbyte *jni_byte_responseData = (jbyte*) (*env)->GetByteArrayElements(env, data, &isCopy);
3524 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback - raw data received : %p",
3525 jni_byte_responseData);
3527 uint8_t* receivedData = OICMalloc(length);
3530 OIC_LOG(ERROR, TAG, "receivedData is null");
3534 memcpy(receivedData, jni_byte_responseData, length);
3535 (*env)->ReleaseByteArrayElements(env, data, jni_byte_responseData, JNI_ABORT);
3537 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3540 OIC_LOG(ERROR, TAG, "jni_address is null");
3541 OICFree(receivedData);
3545 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3548 OIC_LOG(ERROR, TAG, "address is null");
3549 OICFree(receivedData);
3553 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback - data. : %p, %d",
3554 receivedData, length);
3556 ca_mutex_lock(g_bleServerBDAddressMutex);
3557 uint32_t sentLength = 0;
3558 g_CABLEClientDataReceivedCallback(address, receivedData, length,
3560 ca_mutex_unlock(g_bleServerBDAddressMutex);
3562 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3566 * Class: org_iotivity_ca_jar_caleinterface
3567 * Method: CALeGattDescriptorWriteCallback
3568 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattDescriptor;I)V
3570 JNIEXPORT void JNICALL
3571 Java_org_iotivity_ca_CaLeClientInterface_caLeGattDescriptorWriteCallback(JNIEnv *env, jobject obj,
3575 OIC_LOG_V(DEBUG, TAG, "CALeGattDescriptorWriteCallback - status %d: ", status);
3576 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3577 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3578 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3580 CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
3581 if (CA_STATUS_OK != res)
3583 OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
3591 CALEClientSendFinish(env, gatt);