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 CALEClientReorderingList(index, g_deviceList);
2225 ca_mutex_unlock(g_deviceListMutex);
2226 return CA_STATUS_OK;
2228 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2229 (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
2232 ca_mutex_unlock(g_deviceListMutex);
2233 OIC_LOG(DEBUG, TAG, "There are no object in the device list");
2235 return CA_STATUS_OK;
2242 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt)
2244 OIC_LOG(DEBUG, TAG, "CALEClientAddGattobjToList");
2245 VERIFY_NON_NULL(env, TAG, "env is null");
2246 VERIFY_NON_NULL(gatt, TAG, "gatt is null");
2248 ca_mutex_lock(g_gattObjectMutex);
2250 jstring jni_remoteAddress = CALEClientGetAddressFromGattObj(env, gatt);
2251 if (!jni_remoteAddress)
2253 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
2254 ca_mutex_unlock(g_gattObjectMutex);
2255 return CA_STATUS_FAILED;
2258 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2261 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2262 ca_mutex_unlock(g_gattObjectMutex);
2263 return CA_STATUS_FAILED;
2266 if (!CALEClientIsGattObjInList(env, remoteAddress))
2268 jobject newGatt = (*env)->NewGlobalRef(env, gatt);
2269 u_arraylist_add(g_gattObjectList, newGatt);
2270 OIC_LOG(DEBUG, TAG, "Set GATT Object to Array as Element");
2273 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2274 ca_mutex_unlock(g_gattObjectMutex);
2275 return CA_STATUS_OK;
2278 bool CALEClientIsGattObjInList(JNIEnv *env, const char* remoteAddress)
2280 OIC_LOG(DEBUG, TAG, "CALEClientIsGattObjInList");
2281 VERIFY_NON_NULL(env, TAG, "env is null");
2282 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", true);
2284 uint32_t length = u_arraylist_length(g_gattObjectList);
2285 for (uint32_t index = 0; index < length; index++)
2288 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2291 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2295 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2296 if (!jni_setAddress)
2298 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2302 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2305 OIC_LOG(ERROR, TAG, "setAddress is null");
2309 if (!strcmp(remoteAddress, setAddress))
2311 OIC_LOG(DEBUG, TAG, "the device is already set");
2312 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2317 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2322 OIC_LOG(DEBUG, TAG, "There are no GATT object in list. it can be added");
2326 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress)
2328 OIC_LOG(DEBUG, TAG, "CALEClientGetGattObjInList");
2329 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
2330 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", NULL);
2332 ca_mutex_lock(g_gattObjectMutex);
2333 uint32_t length = u_arraylist_length(g_gattObjectList);
2334 for (uint32_t index = 0; index < length; index++)
2336 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2339 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2340 ca_mutex_unlock(g_gattObjectMutex);
2344 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2345 if (!jni_setAddress)
2347 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2348 ca_mutex_unlock(g_gattObjectMutex);
2352 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2355 OIC_LOG(ERROR, TAG, "setAddress is null");
2356 ca_mutex_unlock(g_gattObjectMutex);
2360 if (!strcmp(remoteAddress, setAddress))
2362 OIC_LOG(DEBUG, TAG, "the device is already set");
2363 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2364 ca_mutex_unlock(g_gattObjectMutex);
2367 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2370 ca_mutex_unlock(g_gattObjectMutex);
2371 OIC_LOG(DEBUG, TAG, "There are no the gatt object in list");
2375 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env)
2377 OIC_LOG(DEBUG, TAG, "CALEClientRemoveAllGattObjs");
2378 VERIFY_NON_NULL(env, TAG, "env is null");
2380 ca_mutex_lock(g_gattObjectMutex);
2381 if (!g_gattObjectList)
2383 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
2384 ca_mutex_unlock(g_gattObjectMutex);
2385 return CA_STATUS_FAILED;
2388 uint32_t length = u_arraylist_length(g_gattObjectList);
2389 for (uint32_t index = 0; index < length; index++)
2391 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2394 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2397 (*env)->DeleteGlobalRef(env, jarrayObj);
2400 OICFree(g_gattObjectList);
2401 g_gattObjectList = NULL;
2402 ca_mutex_unlock(g_gattObjectMutex);
2403 return CA_STATUS_OK;
2406 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt)
2408 OIC_LOG(DEBUG, TAG, "CALEClientRemoveGattObj");
2409 VERIFY_NON_NULL(gatt, TAG, "gatt is null");
2410 VERIFY_NON_NULL(env, TAG, "env is null");
2412 ca_mutex_lock(g_gattObjectMutex);
2413 if (!g_gattObjectList)
2415 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
2416 ca_mutex_unlock(g_gattObjectMutex);
2417 return CA_STATUS_FAILED;
2420 uint32_t length = u_arraylist_length(g_gattObjectList);
2421 for (uint32_t index = 0; index < length; index++)
2423 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2426 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2427 ca_mutex_unlock(g_gattObjectMutex);
2428 return CA_STATUS_FAILED;
2431 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2432 if (!jni_setAddress)
2434 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2435 ca_mutex_unlock(g_gattObjectMutex);
2436 return CA_STATUS_FAILED;
2439 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2442 OIC_LOG(ERROR, TAG, "setAddress is null");
2443 ca_mutex_unlock(g_gattObjectMutex);
2444 return CA_STATUS_FAILED;
2447 jstring jni_remoteAddress = CALEClientGetAddressFromGattObj(env, gatt);
2448 if (!jni_remoteAddress)
2450 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
2451 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2452 ca_mutex_unlock(g_gattObjectMutex);
2453 return CA_STATUS_FAILED;
2456 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2459 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2460 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2461 ca_mutex_unlock(g_gattObjectMutex);
2462 return CA_STATUS_FAILED;
2465 if (!strcmp(setAddress, remoteAddress))
2467 OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
2468 (*env)->DeleteGlobalRef(env, jarrayObj);
2469 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2470 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2471 ca_mutex_unlock(g_gattObjectMutex);
2472 return CALEClientReorderingList(index, g_gattObjectList);
2474 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2475 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2478 ca_mutex_unlock(g_gattObjectMutex);
2479 OIC_LOG(DEBUG, TAG, "there are no target object");
2480 return CA_STATUS_OK;
2483 CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr)
2485 OIC_LOG(DEBUG, TAG, "CALEClientRemoveGattObjForAddr");
2486 VERIFY_NON_NULL(addr, TAG, "addr is null");
2487 VERIFY_NON_NULL(env, TAG, "env is null");
2489 ca_mutex_lock(g_gattObjectMutex);
2490 if (!g_gattObjectList)
2492 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
2493 ca_mutex_unlock(g_gattObjectMutex);
2494 return CA_STATUS_FAILED;
2497 uint32_t length = u_arraylist_length(g_gattObjectList);
2498 for (uint32_t index = 0; index < length; index++)
2500 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2503 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2504 ca_mutex_unlock(g_gattObjectMutex);
2505 return CA_STATUS_FAILED;
2508 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2509 if (!jni_setAddress)
2511 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2512 ca_mutex_unlock(g_gattObjectMutex);
2513 return CA_STATUS_FAILED;
2516 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2519 OIC_LOG(ERROR, TAG, "setAddress is null");
2520 ca_mutex_unlock(g_gattObjectMutex);
2521 return CA_STATUS_FAILED;
2524 const char* remoteAddress = (*env)->GetStringUTFChars(env, addr, NULL);
2527 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2528 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2529 ca_mutex_unlock(g_gattObjectMutex);
2530 return CA_STATUS_FAILED;
2533 if (!strcmp(setAddress, remoteAddress))
2535 OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
2536 (*env)->DeleteGlobalRef(env, jarrayObj);
2538 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2539 (*env)->ReleaseStringUTFChars(env, addr, remoteAddress);
2540 ca_mutex_unlock(g_gattObjectMutex);
2541 return CALEClientReorderingList(index, g_gattObjectList);
2543 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2544 (*env)->ReleaseStringUTFChars(env, addr, remoteAddress);
2547 ca_mutex_unlock(g_gattObjectMutex);
2548 OIC_LOG(DEBUG, TAG, "there are no target object");
2549 return CA_STATUS_FAILED;
2556 CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
2557 uint16_t notificationState, uint16_t sendState)
2559 VERIFY_NON_NULL(address, TAG, "address is null");
2561 CALEState_t *newstate = (CALEState_t*) OICMalloc(sizeof(CALEState_t));
2564 OIC_LOG(ERROR, TAG, "out of memory");
2565 return CA_MEMORY_ALLOC_FAILED;
2568 if (strlen(address) > CA_MACADDR_SIZE)
2570 OIC_LOG(ERROR, TAG, "address is not proper");
2572 return CA_STATUS_FAILED;
2575 OICStrcpy(newstate->address, sizeof(newstate->address), address);
2576 newstate->connectedState = connectedState;
2577 newstate->notificationState = notificationState;
2578 newstate->sendState = sendState;
2579 return CALEClientAddDeviceStateToList(newstate);
2582 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state)
2584 VERIFY_NON_NULL(state, TAG, "state is null");
2586 ca_mutex_lock(g_deviceStateListMutex);
2588 if (!g_deviceStateList)
2590 OIC_LOG(ERROR, TAG, "gdevice_list is null");
2591 ca_mutex_unlock(g_deviceStateListMutex);
2592 return CA_STATUS_FAILED;
2595 if (CALEClientIsDeviceInList(state->address))
2597 CALEState_t* curState = CALEClientGetStateInfo(state->address);
2600 OIC_LOG(ERROR, TAG, "curState is null");
2601 ca_mutex_unlock(g_deviceStateListMutex);
2602 return CA_STATUS_FAILED;
2605 if (STATE_CHARACTER_NO_CHANGE == state->notificationState)
2607 state->notificationState = curState->notificationState;
2610 // delete previous state for update new state
2611 CAResult_t res = CALEClientRemoveDeviceState(state->address);
2612 if (CA_STATUS_OK != res)
2614 OIC_LOG(ERROR, TAG, "CALEClientRemoveDeviceState has failed");
2615 ca_mutex_unlock(g_deviceStateListMutex);
2619 u_arraylist_add(g_deviceStateList, state); // update new state
2620 OIC_LOG_V(DEBUG, TAG, "Set State Info to List : %d, %d",
2621 state->connectedState, state->notificationState);
2623 ca_mutex_unlock(g_deviceStateListMutex);
2624 return CA_STATUS_OK;
2627 bool CALEClientIsDeviceInList(const char* remoteAddress)
2629 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
2631 if (!g_deviceStateList)
2633 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2637 uint32_t length = u_arraylist_length(g_deviceStateList);
2638 for (uint32_t index = 0; index < length; index++)
2640 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2643 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2647 if (!strcmp(remoteAddress, state->address))
2649 OIC_LOG(DEBUG, TAG, "the device is already set");
2658 OIC_LOG(DEBUG, TAG, "there are no the device in list.");
2662 CAResult_t CALEClientRemoveAllDeviceState()
2664 OIC_LOG(DEBUG, TAG, "CALENativeRemoveAllDevices");
2666 ca_mutex_lock(g_deviceStateListMutex);
2667 if (!g_deviceStateList)
2669 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2670 ca_mutex_unlock(g_deviceStateListMutex);
2671 return CA_STATUS_FAILED;
2674 uint32_t length = u_arraylist_length(g_deviceStateList);
2675 for (uint32_t index = 0; index < length; index++)
2677 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2680 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2686 OICFree(g_deviceStateList);
2687 g_deviceStateList = NULL;
2688 ca_mutex_unlock(g_deviceStateListMutex);
2690 return CA_STATUS_OK;
2693 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress)
2695 OIC_LOG(DEBUG, TAG, "CALEClientRemoveDeviceState");
2696 VERIFY_NON_NULL(remoteAddress, TAG, "remoteAddress is null");
2698 if (!g_deviceStateList)
2700 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2701 return CA_STATUS_FAILED;
2704 uint32_t length = u_arraylist_length(g_deviceStateList);
2705 for (uint32_t index = 0; index < length; index++)
2707 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2710 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2714 if (!strcmp(state->address, remoteAddress))
2716 OIC_LOG_V(DEBUG, TAG, "remove state : %s", remoteAddress);
2719 CAResult_t res = CALEClientReorderingList(index, g_deviceStateList);
2720 if(CA_STATUS_OK != res)
2722 OIC_LOG(ERROR, TAG, "CALEClientReorderingList has failed");
2725 return CA_STATUS_OK;
2729 return CA_STATUS_FAILED;
2732 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress)
2734 OIC_LOG(DEBUG, TAG, "CALEClientGetStateInfo");
2735 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", NULL);
2737 if (!g_deviceStateList)
2739 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2743 uint32_t length = u_arraylist_length(g_deviceStateList);
2744 for (uint32_t index = 0; index < length; index++)
2746 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2749 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2753 if (!strcmp(state->address, remoteAddress))
2755 OIC_LOG_V(DEBUG, TAG, "get state : %s", remoteAddress);
2762 bool CALEClientIsConnectedDevice(const char* remoteAddress)
2764 OIC_LOG(DEBUG, TAG, "CALEClientIsConnectedDevice");
2765 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
2767 ca_mutex_lock(g_deviceStateListMutex);
2768 if (!g_deviceStateList)
2770 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2771 ca_mutex_unlock(g_deviceStateListMutex);
2775 uint32_t length = u_arraylist_length(g_deviceStateList);
2776 for (uint32_t index = 0; index < length; index++)
2778 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2781 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2785 if (!strcmp(state->address, remoteAddress))
2787 OIC_LOG(DEBUG, TAG, "check whether it is connected or not");
2789 if (STATE_CONNECTED == state->connectedState)
2791 ca_mutex_unlock(g_deviceStateListMutex);
2796 ca_mutex_unlock(g_deviceStateListMutex);
2801 ca_mutex_unlock(g_deviceStateListMutex);
2805 bool CALEClientIsSetCharacteristic(const char* remoteAddress)
2807 OIC_LOG(DEBUG, TAG, "CALEClientIsSetCharacteristic");
2808 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
2810 ca_mutex_lock(g_deviceStateListMutex);
2811 if (!g_deviceStateList)
2813 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2814 ca_mutex_unlock(g_deviceStateListMutex);
2818 uint32_t length = u_arraylist_length(g_deviceStateList);
2819 for (uint32_t index = 0; index < length; index++)
2821 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2824 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2828 if (!strcmp(state->address, remoteAddress))
2830 OIC_LOG_V(DEBUG, TAG, "check whether it was set or not:%d", state->notificationState);
2832 if (STATE_CHARACTER_SET == state->notificationState)
2834 ca_mutex_unlock(g_deviceStateListMutex);
2839 ca_mutex_unlock(g_deviceStateListMutex);
2845 ca_mutex_unlock(g_deviceStateListMutex);
2849 void CALEClientCreateDeviceList()
2851 OIC_LOG(DEBUG, TAG, "CALEClientCreateDeviceList");
2853 // create new object array
2854 if (!g_gattObjectList)
2856 OIC_LOG(DEBUG, TAG, "Create g_gattObjectList");
2858 g_gattObjectList = u_arraylist_create();
2861 if (!g_deviceStateList)
2863 OIC_LOG(DEBUG, TAG, "Create g_deviceStateList");
2865 g_deviceStateList = u_arraylist_create();
2870 OIC_LOG(DEBUG, TAG, "Create g_deviceList");
2872 g_deviceList = u_arraylist_create();
2876 CAResult_t CALEClientReorderingList(uint32_t index, u_arraylist_t *list)
2880 OIC_LOG(ERROR, TAG, "list is null");
2881 return CA_STATUS_FAILED;
2884 if (index >= list->length)
2886 OIC_LOG(ERROR, TAG, "index is not available");
2887 return CA_STATUS_FAILED;
2890 if (index < list->length - 1)
2892 memmove(&list->data[index], &list->data[index + 1],
2893 (list->length - index - 1) * sizeof(void *));
2899 return CA_STATUS_OK;
2903 * Check Sent Count for remove g_sendBuffer
2905 void CALEClientUpdateSendCnt(JNIEnv *env)
2907 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
2909 ca_mutex_lock(g_threadMutex);
2913 if (g_targetCnt <= g_currentSentCnt)
2916 g_currentSentCnt = 0;
2920 (*env)->DeleteGlobalRef(env, g_sendBuffer);
2921 g_sendBuffer = NULL;
2923 // notity the thread
2924 ca_cond_signal(g_threadCond);
2925 CALEClientSetSendFinishFlag(true);
2926 OIC_LOG(DEBUG, TAG, "set signal for send data");
2929 ca_mutex_unlock(g_threadMutex);
2932 CAResult_t CALEClientInitGattMutexVaraibles()
2934 OIC_LOG(DEBUG, TAG, "IN");
2936 if (NULL == g_bleReqRespClientCbMutex)
2938 g_bleReqRespClientCbMutex = ca_mutex_new();
2939 if (NULL == g_bleReqRespClientCbMutex)
2941 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2942 return CA_STATUS_FAILED;
2946 if (NULL == g_bleServerBDAddressMutex)
2948 g_bleServerBDAddressMutex = ca_mutex_new();
2949 if (NULL == g_bleServerBDAddressMutex)
2951 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2952 return CA_STATUS_FAILED;
2956 if (NULL == g_threadMutex)
2958 g_threadMutex = ca_mutex_new();
2959 if (NULL == g_threadMutex)
2961 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2962 return CA_STATUS_FAILED;
2966 if (NULL == g_threadSendMutex)
2968 g_threadSendMutex = ca_mutex_new();
2969 if (NULL == g_threadSendMutex)
2971 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2972 return CA_STATUS_FAILED;
2976 if (NULL == g_deviceListMutex)
2978 g_deviceListMutex = ca_mutex_new();
2979 if (NULL == g_deviceListMutex)
2981 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2982 return CA_STATUS_FAILED;
2986 if (NULL == g_gattObjectMutex)
2988 g_gattObjectMutex = ca_mutex_new();
2989 if (NULL == g_gattObjectMutex)
2991 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2992 return CA_STATUS_FAILED;
2996 if (NULL == g_deviceStateListMutex)
2998 g_deviceStateListMutex = ca_mutex_new();
2999 if (NULL == g_deviceStateListMutex)
3001 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3002 return CA_STATUS_FAILED;
3006 if (NULL == g_SendFinishMutex)
3008 g_SendFinishMutex = ca_mutex_new();
3009 if (NULL == g_SendFinishMutex)
3011 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3012 return CA_STATUS_FAILED;
3016 OIC_LOG(DEBUG, TAG, "OUT");
3017 return CA_STATUS_OK;
3020 void CALEClientTerminateGattMutexVariables()
3022 OIC_LOG(DEBUG, TAG, "IN");
3024 ca_mutex_free(g_bleReqRespClientCbMutex);
3025 g_bleReqRespClientCbMutex = NULL;
3027 ca_mutex_free(g_bleServerBDAddressMutex);
3028 g_bleServerBDAddressMutex = NULL;
3030 ca_mutex_free(g_threadMutex);
3031 g_threadMutex = NULL;
3033 ca_mutex_free(g_threadSendMutex);
3034 g_threadSendMutex = NULL;
3036 ca_mutex_free(g_deviceListMutex);
3037 g_deviceListMutex = NULL;
3039 ca_mutex_free(g_SendFinishMutex);
3040 g_SendFinishMutex = NULL;
3042 OIC_LOG(DEBUG, TAG, "OUT");
3045 void CALEClientSetSendFinishFlag(bool flag)
3047 OIC_LOG_V(DEBUG, TAG, "g_isFinishedSendData is %d", flag);
3049 ca_mutex_lock(g_SendFinishMutex);
3050 g_isFinishedSendData = flag;
3051 ca_mutex_unlock(g_SendFinishMutex);
3058 CAResult_t CAStartLEGattClient()
3060 CAResult_t res = CALEClientStartMulticastServer();
3061 if (CA_STATUS_OK != res)
3063 OIC_LOG(ERROR, TAG, "CALEClientStartMulticastServer has failed");
3067 g_isStartedLEClient = true;
3073 void CAStopLEGattClient()
3075 OIC_LOG(DEBUG, TAG, "CAStopBLEGattClient");
3079 OIC_LOG(ERROR, TAG, "g_jvm is null");
3083 bool isAttached = false;
3085 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
3088 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
3089 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
3093 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
3099 CAResult_t ret = CALEClientDisconnectAll(env);
3100 if (CA_STATUS_OK != ret)
3102 OIC_LOG(ERROR, TAG, "CALEClientDisconnectAll has failed");
3105 ret = CALEClientStopScan();
3106 if(CA_STATUS_OK != ret)
3108 OIC_LOG(ERROR, TAG, "CALEClientStopScan has failed");
3111 ca_cond_signal(g_threadCond);
3115 (*g_jvm)->DetachCurrentThread(g_jvm);
3120 void CATerminateLEGattClient()
3122 OIC_LOG(DEBUG, TAG, "Terminate GATT Client");
3123 CALEClientTerminate();
3126 CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress, const uint8_t *data,
3127 uint32_t dataLen, CALETransferType_t type,
3130 OIC_LOG(DEBUG, TAG, "call CALEClientSendUnicastMessage");
3131 VERIFY_NON_NULL(data, TAG, "data is null");
3132 VERIFY_NON_NULL(remoteAddress, TAG, "remoteAddress is null");
3134 if (LE_UNICAST != type || position < 0)
3136 OIC_LOG(ERROR, TAG, "this request is not unicast");
3137 return CA_STATUS_INVALID_PARAM;
3140 return CALEClientSendUnicastMessage(remoteAddress, data, dataLen);
3143 CAResult_t CAUpdateCharacteristicsToAllGattServers(const uint8_t *data, uint32_t dataLen)
3145 OIC_LOG(DEBUG, TAG, "call CALEClientSendMulticastMessage");
3146 VERIFY_NON_NULL(data, TAG, "data is null");
3148 return CALEClientSendMulticastMessage(data, dataLen);
3151 void CASetLEReqRespClientCallback(CABLEDataReceivedCallback callback)
3153 OIC_LOG(DEBUG, TAG, "IN");
3155 ca_mutex_lock(g_bleReqRespClientCbMutex);
3156 g_CABLEClientDataReceivedCallback = callback;
3157 ca_mutex_unlock(g_bleReqRespClientCbMutex);
3159 OIC_LOG(DEBUG, TAG, "OUT");
3162 void CASetLEClientThreadPoolHandle(ca_thread_pool_t handle)
3164 OIC_LOG(DEBUG, TAG, "IN");
3166 CALEClientInitialize(handle);
3168 OIC_LOG(DEBUG, TAG, "OUT");
3171 CAResult_t CAGetLEAddress(char **local_address)
3173 VERIFY_NON_NULL(local_address, TAG, "local_address");
3174 OIC_LOG(INFO, TAG, "CAGetLEAddress is not support");
3175 return CA_NOT_SUPPORTED;
3178 JNIEXPORT void JNICALL
3179 Java_org_iotivity_ca_CaLeClientInterface_caLeRegisterLeScanCallback(JNIEnv *env, jobject obj,
3182 OIC_LOG(DEBUG, TAG, "CaLeRegisterLeScanCallback");
3183 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3184 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3185 VERIFY_NON_NULL_VOID(callback, TAG, "callback is null");
3187 g_leScanCallback = (*env)->NewGlobalRef(env, callback);
3190 JNIEXPORT void JNICALL
3191 Java_org_iotivity_ca_CaLeClientInterface_caLeRegisterGattCallback(JNIEnv *env, jobject obj,
3194 OIC_LOG(DEBUG, TAG, "CaLeRegisterGattCallback");
3195 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3196 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3197 VERIFY_NON_NULL_VOID(callback, TAG, "callback is null");
3199 g_leGattCallback = (*env)->NewGlobalRef(env, callback);
3202 JNIEXPORT void JNICALL
3203 Java_org_iotivity_ca_CaLeClientInterface_caLeScanCallback(JNIEnv *env, jobject obj,
3206 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3207 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3208 VERIFY_NON_NULL_VOID(device, TAG, "device is null");
3210 CAResult_t res = CALEClientAddScanDeviceToList(env, device);
3211 if (CA_STATUS_OK != res)
3213 OIC_LOG_V(ERROR, TAG, "CALEClientAddScanDeviceToList has failed : %d", res);
3218 * Class: org_iotivity_ca_jar_caleinterface
3219 * Method: CALeGattConnectionStateChangeCallback
3220 * Signature: (Landroid/bluetooth/BluetoothGatt;II)V
3222 JNIEXPORT void JNICALL
3223 Java_org_iotivity_ca_CaLeClientInterface_caLeGattConnectionStateChangeCallback(JNIEnv *env,
3229 OIC_LOG_V(DEBUG, TAG, "CALeGattConnectionStateChangeCallback - status %d, newstate %d", status,
3231 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3232 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3233 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3235 if (GATT_SUCCESS == status && STATE_CONNECTED == newstate) // le connected
3237 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3243 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3246 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED,
3247 STATE_CHARACTER_NO_CHANGE,
3249 if (CA_STATUS_OK != res)
3251 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3252 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3255 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3258 CAResult_t res = CALEClientAddGattobjToList(env, gatt);
3259 if (CA_STATUS_OK != res)
3261 OIC_LOG(ERROR, TAG, "CALEClientAddGattobjToList has failed");
3265 res = CALEClientDiscoverServices(env, gatt);
3266 if (CA_STATUS_OK != res)
3268 OIC_LOG(ERROR, TAG, "CALEClientDiscoverServices has failed");
3272 else if (GATT_SUCCESS == status && STATE_DISCONNECTED == newstate) // le disconnected
3274 CAResult_t res = CALEClientStartScan();
3275 if (CA_STATUS_OK != res)
3277 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
3280 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3283 OIC_LOG(ERROR, TAG, "CALEClientGetAddressFromGattObj has failed");
3286 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3289 res = CALEClientUpdateDeviceState(address, STATE_DISCONNECTED,
3290 STATE_CHARACTER_NO_CHANGE,
3292 if (CA_STATUS_OK != res)
3294 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3296 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3299 res = CALEClientGattClose(env, gatt);
3300 if (CA_STATUS_OK != res)
3302 OIC_LOG(ERROR, TAG, "CALEClientGattClose has failed");
3308 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3311 OIC_LOG(ERROR, TAG, "jni_address is null");
3316 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3319 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_DISCONNECTED,
3320 STATE_CHARACTER_NO_CHANGE,
3322 if (CA_STATUS_OK != res)
3324 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3327 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3329 CAResult_t res = CALEClientGattClose(env, gatt);
3330 if (CA_STATUS_OK != res)
3332 OIC_LOG(ERROR, TAG, "CALEClientGattClose has failed");
3342 CALEClientSendFinish(env, gatt);
3347 * Class: org_iotivity_ca_jar_caleinterface
3348 * Method: CALeGattServicesDiscoveredCallback
3349 * Signature: (Landroid/bluetooth/BluetoothGatt;I)V
3351 JNIEXPORT void JNICALL
3352 Java_org_iotivity_ca_CaLeClientInterface_caLeGattServicesDiscoveredCallback(JNIEnv *env,
3357 OIC_LOG_V(DEBUG, TAG, "CALeGattServicesDiscoveredCallback - status %d: ", status);
3358 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3359 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3360 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3362 if (0 != status) // discovery error
3364 CALEClientSendFinish(env, gatt);
3368 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3371 CALEClientSendFinish(env, gatt);
3375 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3378 CALEClientSendFinish(env, gatt);
3382 if (!CALEClientIsSetCharacteristic(address))
3384 jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_RESPONSE_UUID);
3387 OIC_LOG(ERROR, TAG, "jni_uuid is null");
3391 jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, gatt, jni_uuid);
3392 if (!jni_obj_GattCharacteristic)
3394 OIC_LOG(ERROR, TAG, "jni_obj_GattCharacteristic is null");
3398 CAResult_t res = CALEClientSetCharacteristicNotification(env, gatt,
3399 jni_obj_GattCharacteristic);
3400 if (CA_STATUS_OK != res)
3402 OIC_LOG(ERROR, TAG, "CALEClientSetCharacteristicNotification has failed");
3406 res = CALEClientSetUUIDToDescriptor(env, gatt, jni_obj_GattCharacteristic);
3407 if (CA_STATUS_OK != res)
3409 OIC_LOG(INFO, TAG, "Descriptor of the uuid is not found");
3410 CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
3411 if (CA_STATUS_OK != res)
3413 OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
3418 res = CALEClientUpdateDeviceState(address, STATE_CONNECTED, STATE_CHARACTER_SET,
3420 if (CA_STATUS_OK != res)
3422 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3428 CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
3429 if (CA_STATUS_OK != res)
3431 OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
3435 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3440 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3441 CALEClientSendFinish(env, gatt);
3446 * Class: org_iotivity_ca_jar_caleinterface
3447 * Method: CALeGattCharacteristicWritjclasseCallback
3448 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;I)V
3450 JNIEXPORT void JNICALL
3451 Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicWriteCallback(
3452 JNIEnv *env, jobject obj, jobject gatt, jbyteArray data,
3455 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - status : %d", status);
3456 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3457 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3458 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3461 char* wroteData = (char*) (*env)->GetByteArrayElements(env, data, &isCopy);
3463 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - write data : %s", wroteData);
3465 // send success & signal
3466 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3472 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3478 if (GATT_SUCCESS != status) // error case
3480 OIC_LOG(ERROR, TAG, "send failure");
3481 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED, STATE_CHARACTER_SET,
3483 if (CA_STATUS_OK != res)
3485 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3487 CALEClientSendFinish(env, gatt);
3491 OIC_LOG(DEBUG, TAG, "send success");
3492 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED, STATE_CHARACTER_SET,
3493 STATE_SEND_SUCCESS);
3494 if (CA_STATUS_OK != res)
3496 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3498 CALEClientUpdateSendCnt(env);
3501 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3507 CALEClientSendFinish(env, gatt);
3512 * Class: org_iotivity_ca_jar_caleinterface
3513 * Method: CALeGattCharacteristicChangedCallback
3514 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;)V
3516 JNIEXPORT void JNICALL
3517 Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicChangedCallback(
3518 JNIEnv *env, jobject obj, jobject gatt, jbyteArray data)
3520 OIC_LOG(DEBUG, TAG, "CALeGattCharacteristicChangedCallback");
3521 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3522 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3523 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3524 VERIFY_NON_NULL_VOID(data, TAG, "data is null");
3526 // get Byte Array and convert to uint8_t*
3527 jint length = (*env)->GetArrayLength(env, data);
3530 jbyte *jni_byte_responseData = (jbyte*) (*env)->GetByteArrayElements(env, data, &isCopy);
3532 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback - raw data received : %p",
3533 jni_byte_responseData);
3535 uint8_t* receivedData = OICMalloc(length);
3538 OIC_LOG(ERROR, TAG, "receivedData is null");
3542 memcpy(receivedData, jni_byte_responseData, length);
3543 (*env)->ReleaseByteArrayElements(env, data, jni_byte_responseData, JNI_ABORT);
3545 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3548 OIC_LOG(ERROR, TAG, "jni_address is null");
3549 OICFree(receivedData);
3553 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3556 OIC_LOG(ERROR, TAG, "address is null");
3557 OICFree(receivedData);
3561 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback - data. : %p, %d",
3562 receivedData, length);
3564 ca_mutex_lock(g_bleServerBDAddressMutex);
3565 uint32_t sentLength = 0;
3566 g_CABLEClientDataReceivedCallback(address, receivedData, length,
3568 ca_mutex_unlock(g_bleServerBDAddressMutex);
3570 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3574 * Class: org_iotivity_ca_jar_caleinterface
3575 * Method: CALeGattDescriptorWriteCallback
3576 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattDescriptor;I)V
3578 JNIEXPORT void JNICALL
3579 Java_org_iotivity_ca_CaLeClientInterface_caLeGattDescriptorWriteCallback(JNIEnv *env, jobject obj,
3583 OIC_LOG_V(DEBUG, TAG, "CALeGattDescriptorWriteCallback - status %d: ", status);
3584 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3585 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3586 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3588 CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
3589 if (CA_STATUS_OK != res)
3591 OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
3599 CALEClientSendFinish(env, gatt);