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("OIC_CA_LE_CLIENT")
42 #define MICROSECS_PER_SEC 1000000
43 #define WAIT_TIME_WRITE_CHARACTERISTIC 10 * MICROSECS_PER_SEC
45 static const char METHODID_OBJECTNONPARAM[] = "()Landroid/bluetooth/BluetoothAdapter;";
46 static const char CLASSPATH_BT_ADAPTER[] = "android/bluetooth/BluetoothAdapter";
47 static const char CLASSPATH_BT_UUID[] = "java/util/UUID";
48 static const char CLASSPATH_BT_GATT[] = "android/bluetooth/BluetoothGatt";
50 static ca_thread_pool_t g_threadPoolHandle = NULL;
53 static u_arraylist_t *g_deviceList = NULL; // device list to have same UUID
54 static u_arraylist_t *g_gattObjectList = NULL;
55 static u_arraylist_t *g_deviceStateList = NULL;
57 static CAPacketReceiveCallback g_packetReceiveCallback = NULL;
58 static CABLEErrorHandleCallback g_clientErrorCallback;
59 static jobject g_leScanCallback = NULL;
60 static jobject g_leGattCallback = NULL;
61 static jobject g_context = NULL;
62 static jobjectArray g_uuidList = NULL;
64 // it will be prevent to start send logic when adapter has stopped.
65 static bool g_isStartedLEClient = false;
66 static bool g_isStartedMulticastServer = false;
67 static bool g_isStartedScan = false;
69 static jbyteArray g_sendBuffer = NULL;
70 static uint32_t g_targetCnt = 0;
71 static uint32_t g_currentSentCnt = 0;
72 static bool g_isFinishedSendData = false;
73 static ca_mutex g_SendFinishMutex = NULL;
74 static ca_mutex g_threadMutex = NULL;
75 static ca_cond g_threadCond = NULL;
76 static ca_cond g_deviceDescCond = NULL;
78 static ca_mutex g_threadSendMutex = NULL;
79 static ca_mutex g_threadWriteCharacteristicMutex = NULL;
80 static ca_cond g_threadWriteCharacteristicCond = NULL;
81 static bool g_isSignalSetFlag = false;
83 static ca_mutex g_bleReqRespClientCbMutex = NULL;
84 static ca_mutex g_bleServerBDAddressMutex = NULL;
86 static ca_mutex g_deviceListMutex = NULL;
87 static ca_mutex g_gattObjectMutex = NULL;
88 static ca_mutex g_deviceStateListMutex = NULL;
90 static ca_mutex g_scanMutex = NULL;
92 static CABLEDataReceivedCallback g_CABLEClientDataReceivedCallback = NULL;
95 void CALEClientJniInit()
97 OIC_LOG(DEBUG, TAG, "CALEClientJniInit");
98 g_jvm = (JavaVM*) CANativeJNIGetJavaVM();
101 void CALEClientJNISetContext()
103 OIC_LOG(DEBUG, TAG, "CALEClientJNISetContext");
104 g_context = (jobject) CANativeJNIGetContext();
107 CAResult_t CALECreateJniInterfaceObject()
109 OIC_LOG(DEBUG, TAG, "CALECreateJniInterfaceObject");
113 OIC_LOG(ERROR, TAG, "g_context is null");
114 return CA_STATUS_FAILED;
119 OIC_LOG(ERROR, TAG, "g_jvm is null");
120 return CA_STATUS_FAILED;
123 bool isAttached = false;
125 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
128 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
129 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
133 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
134 return CA_STATUS_FAILED;
139 jclass jni_LEInterface = (*env)->FindClass(env, "org/iotivity/ca/CaLeClientInterface");
140 if (!jni_LEInterface)
142 OIC_LOG(ERROR, TAG, "Could not get CaLeClientInterface class");
146 jmethodID LeInterfaceConstructorMethod = (*env)->GetMethodID(env, jni_LEInterface, "<init>",
147 "(Landroid/content/Context;)V");
148 if (!LeInterfaceConstructorMethod)
150 OIC_LOG(ERROR, TAG, "Could not get CaLeClientInterface constructor method");
154 (*env)->NewObject(env, jni_LEInterface, LeInterfaceConstructorMethod, g_context);
155 OIC_LOG(DEBUG, TAG, "Create instance for CaLeClientInterface");
159 (*g_jvm)->DetachCurrentThread(g_jvm);
168 (*g_jvm)->DetachCurrentThread(g_jvm);
171 return CA_STATUS_FAILED;
174 CAResult_t CALEClientInitialize()
176 OIC_LOG(DEBUG, TAG, "CALEClientInitialize");
182 OIC_LOG(ERROR, TAG, "g_jvm is null");
183 return CA_STATUS_FAILED;
186 bool isAttached = false;
188 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
191 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
192 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
196 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
197 return CA_STATUS_FAILED;
202 CAResult_t ret = CALECheckPlatformVersion(env, 18);
203 if (CA_STATUS_OK != ret)
205 OIC_LOG(ERROR, TAG, "it is not supported");
209 (*g_jvm)->DetachCurrentThread(g_jvm);
215 ret = CALEClientInitGattMutexVaraibles();
216 if (CA_STATUS_OK != ret)
218 OIC_LOG(ERROR, TAG, "CALEClientInitGattMutexVaraibles has failed!");
219 CALEClientTerminateGattMutexVariables();
223 (*g_jvm)->DetachCurrentThread(g_jvm);
229 g_deviceDescCond = ca_cond_new();
231 // init mutex for send logic
232 g_threadCond = ca_cond_new();
233 g_threadWriteCharacteristicCond = ca_cond_new();
235 CALEClientCreateDeviceList();
236 CALEClientJNISetContext();
238 ret = CALEClientCreateUUIDList();
239 if (CA_STATUS_OK != ret)
241 OIC_LOG(ERROR, TAG, "CALEClientCreateUUIDList has failed");
245 (*g_jvm)->DetachCurrentThread(g_jvm);
251 ret = CALECreateJniInterfaceObject(); /* create java caleinterface instance*/
252 if (CA_STATUS_OK != ret)
254 OIC_LOG(ERROR, TAG, "CALECreateJniInterfaceObject has failed");
258 (*g_jvm)->DetachCurrentThread(g_jvm);
263 g_isStartedLEClient = true;
267 (*g_jvm)->DetachCurrentThread(g_jvm);
273 void CALEClientTerminate()
275 OIC_LOG(DEBUG, TAG, "CALEClientTerminate");
279 OIC_LOG(ERROR, TAG, "g_jvm is null");
283 bool isAttached = false;
285 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
288 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
289 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
293 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
299 if (g_leScanCallback)
301 (*env)->DeleteGlobalRef(env, g_leScanCallback);
304 if (g_leGattCallback)
306 (*env)->DeleteGlobalRef(env, g_leGattCallback);
311 (*env)->DeleteGlobalRef(env, g_sendBuffer);
316 (*env)->DeleteGlobalRef(env, g_uuidList);
319 CAResult_t ret = CALEClientRemoveAllDeviceState();
320 if (CA_STATUS_OK != ret)
322 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllDeviceState has failed");
325 ret = CALEClientRemoveAllScanDevices(env);
326 if (CA_STATUS_OK != ret)
328 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllScanDevices has failed");
331 ret = CALEClientRemoveAllGattObjs(env);
332 if (CA_STATUS_OK != ret)
334 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllGattObjs has failed");
337 g_isStartedMulticastServer = false;
338 CALEClientSetScanFlag(false);
339 CALEClientSetSendFinishFlag(false);
341 CALEClientTerminateGattMutexVariables();
342 CALEClientDestroyJniInterface();
344 ca_cond_free(g_deviceDescCond);
345 ca_cond_free(g_threadCond);
346 ca_cond_free(g_threadWriteCharacteristicCond);
348 g_deviceDescCond = NULL;
350 g_threadWriteCharacteristicCond = NULL;
351 g_isSignalSetFlag = false;
355 (*g_jvm)->DetachCurrentThread(g_jvm);
359 CAResult_t CALEClientDestroyJniInterface()
361 OIC_LOG(DEBUG, TAG, "CALEClientDestroyJniInterface");
365 OIC_LOG(ERROR, TAG, "g_jvm is null");
366 return CA_STATUS_FAILED;
369 bool isAttached = false;
371 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
374 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
375 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
379 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
380 return CA_STATUS_FAILED;
385 jclass jni_LeInterface = (*env)->FindClass(env, "org/iotivity/ca/CaLeClientInterface");
386 if (!jni_LeInterface)
388 OIC_LOG(ERROR, TAG, "Could not get CaLeClientInterface class");
392 jmethodID jni_InterfaceDestroyMethod = (*env)->GetStaticMethodID(env, jni_LeInterface,
393 "destroyLeInterface",
395 if (!jni_InterfaceDestroyMethod)
397 OIC_LOG(ERROR, TAG, "Could not get CaLeClientInterface destroy method");
401 (*env)->CallStaticVoidMethod(env, jni_LeInterface, jni_InterfaceDestroyMethod);
403 if ((*env)->ExceptionCheck(env))
405 OIC_LOG(ERROR, TAG, "destroyLeInterface has failed");
406 (*env)->ExceptionDescribe(env);
407 (*env)->ExceptionClear(env);
411 OIC_LOG(DEBUG, TAG, "Destroy instance for CaLeClientInterface");
415 (*g_jvm)->DetachCurrentThread(g_jvm);
424 (*g_jvm)->DetachCurrentThread(g_jvm);
427 return CA_STATUS_FAILED;
430 void CALEClientSendFinish(JNIEnv *env, jobject gatt)
432 OIC_LOG(DEBUG, TAG, "CALEClientSendFinish");
433 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
437 CAResult_t res = CALEClientDisconnect(env, gatt);
438 if (CA_STATUS_OK != res)
440 OIC_LOG(ERROR, TAG, "CALEClientDisconnect has failed");
443 CALEClientUpdateSendCnt(env);
446 CAResult_t CALEClientSendUnicastMessage(const char* address,
448 const uint32_t dataLen)
450 OIC_LOG_V(DEBUG, TAG, "CALEClientSendUnicastMessage(%s, %p)", address, data);
451 VERIFY_NON_NULL(address, TAG, "address is null");
452 VERIFY_NON_NULL(data, TAG, "data is null");
454 return CALEClientSendUnicastMessageImpl(address, data, dataLen);
457 CAResult_t CALEClientSendMulticastMessage(const uint8_t* data,
458 const uint32_t dataLen)
460 OIC_LOG_V(DEBUG, TAG, "CALEClientSendMulticastMessage(%p)", data);
461 VERIFY_NON_NULL(data, TAG, "data is null");
465 OIC_LOG(ERROR, TAG, "g_jvm is null");
466 return CA_STATUS_FAILED;
469 bool isAttached = false;
471 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
474 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
475 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
479 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
480 return CA_STATUS_FAILED;
485 CAResult_t ret = CALEClientSendMulticastMessageImpl(env, data, dataLen);
486 if (CA_STATUS_OK != ret)
488 OIC_LOG(ERROR, TAG, "CALEClientSendMulticastMessageImpl has failed");
493 (*g_jvm)->DetachCurrentThread(g_jvm);
499 CAResult_t CALEClientStartUnicastServer(const char* address)
501 OIC_LOG_V(DEBUG, TAG, "it is not needed in this platform (%s)", address);
503 return CA_NOT_SUPPORTED;
506 CAResult_t CALEClientStartMulticastServer()
508 OIC_LOG(DEBUG, TAG, "CALEClientStartMulticastServer");
510 if (g_isStartedMulticastServer)
512 OIC_LOG(ERROR, TAG, "server is already started..it will be skipped");
513 return CA_STATUS_FAILED;
518 OIC_LOG(ERROR, TAG, "g_jvm is null");
519 return CA_STATUS_FAILED;
522 bool isAttached = false;
524 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
527 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
528 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
532 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
533 return CA_STATUS_FAILED;
538 g_isStartedMulticastServer = true;
539 CAResult_t ret = CALEClientStartScan();
540 if (CA_STATUS_OK != ret)
542 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
547 (*g_jvm)->DetachCurrentThread(g_jvm);
553 void CALEClientStopUnicastServer()
555 OIC_LOG(DEBUG, TAG, "CALEClientStopUnicastServer");
558 void CALEClientStopMulticastServer()
560 OIC_LOG(DEBUG, TAG, "CALEClientStopMulticastServer");
561 g_isStartedMulticastServer = false;
562 CAResult_t res = CALEClientStopScan();
563 if (CA_STATUS_OK != res)
565 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
570 void CALEClientSetCallback(CAPacketReceiveCallback callback)
572 g_packetReceiveCallback = callback;
575 void CASetBLEClientErrorHandleCallback(CABLEErrorHandleCallback callback)
577 g_clientErrorCallback = callback;
580 CAResult_t CALEClientIsThereScannedDevices()
584 return CA_STATUS_FAILED;
587 if (0 == u_arraylist_length(g_deviceList))
589 // Wait for LE peripherals to be discovered.
591 // Number of times to wait for discovery to complete.
592 static size_t const RETRIES = 5;
594 static uint64_t const TIMEOUT =
595 2 * MICROSECS_PER_SEC; // Microseconds
597 bool devicesDiscovered = false;
599 0 == u_arraylist_length(g_deviceList) && i < RETRIES;
602 if (ca_cond_wait_for(g_deviceDescCond,
606 devicesDiscovered = true;
611 if (!devicesDiscovered)
613 return CA_STATUS_FAILED;
620 CAResult_t CALEClientSendUnicastMessageImpl(const char* address, const uint8_t* data,
621 const uint32_t dataLen)
623 OIC_LOG_V(DEBUG, TAG, "CALEClientSendUnicastMessageImpl, address: %s, data: %p", address,
625 VERIFY_NON_NULL(address, TAG, "address is null");
626 VERIFY_NON_NULL(data, TAG, "data is null");
630 OIC_LOG(ERROR, TAG, "g_jvm is null");
631 return CA_STATUS_FAILED;
634 bool isAttached = false;
636 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
639 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
640 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
643 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
644 return CA_STATUS_FAILED;
649 ca_mutex_lock(g_threadSendMutex);
651 CALEClientSetSendFinishFlag(false);
653 CAResult_t ret = CALEClientIsThereScannedDevices();
654 if (CA_STATUS_OK != ret)
656 OIC_LOG(INFO, TAG, "there is no scanned device");
660 if (g_context && g_deviceList)
662 uint32_t length = u_arraylist_length(g_deviceList);
663 for (uint32_t index = 0; index < length; index++)
665 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
668 OIC_LOG(ERROR, TAG, "jarrayObj is null");
672 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
675 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
679 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
682 OIC_LOG(ERROR, TAG, "setAddress is null");
686 OIC_LOG_V(DEBUG, TAG, "remote device address is %s", setAddress);
688 if (!strcmp(setAddress, address))
690 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
692 // connect to gatt server
693 ret = CALEClientStopScan();
694 if (CA_STATUS_OK != ret)
696 OIC_LOG(ERROR, TAG, "CALEClientStopScan has failed");
702 (*env)->DeleteGlobalRef(env, g_sendBuffer);
705 jbyteArray jni_arr = (*env)->NewByteArray(env, dataLen);
706 (*env)->SetByteArrayRegion(env, jni_arr, 0, dataLen, (jbyte*) data);
707 g_sendBuffer = (jbyteArray)(*env)->NewGlobalRef(env, jni_arr);
709 // Target device to send message is just one.
712 ret = CALEClientSendData(env, jarrayObj);
713 if (CA_STATUS_OK != ret)
715 OIC_LOG(ERROR, TAG, "CALEClientSendData in unicast is failed");
719 OIC_LOG(INFO, TAG, "wake up");
722 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
726 OIC_LOG(DEBUG, TAG, "connection routine is finished for unicast");
728 // wait for finish to send data through "CALeGattServicesDiscoveredCallback"
729 // if there is no connection state.
730 if (!g_isFinishedSendData)
732 ca_mutex_lock(g_threadMutex);
733 ca_cond_wait(g_threadCond, g_threadMutex);
734 OIC_LOG(DEBUG, TAG, "the data was sent");
735 ca_mutex_unlock(g_threadMutex);
740 (*g_jvm)->DetachCurrentThread(g_jvm);
743 // start LE Scan again
744 ret = CALEClientStartScan();
745 if (CA_STATUS_OK != ret)
747 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
748 ca_mutex_unlock(g_threadSendMutex);
752 ca_mutex_unlock(g_threadSendMutex);
753 OIC_LOG(INFO, TAG, "unicast - send success");
759 // start LE Scan again
760 ret = CALEClientStartScan();
761 if (CA_STATUS_OK != ret)
763 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
764 ca_mutex_unlock(g_threadSendMutex);
767 (*g_jvm)->DetachCurrentThread(g_jvm);
774 (*g_jvm)->DetachCurrentThread(g_jvm);
777 if (g_clientErrorCallback)
779 g_clientErrorCallback(address, data, dataLen, CA_SEND_FAILED);
781 ca_mutex_unlock(g_threadSendMutex);
782 return CA_SEND_FAILED;
785 CAResult_t CALEClientSendMulticastMessageImpl(JNIEnv *env, const uint8_t* data,
786 const uint32_t dataLen)
788 OIC_LOG_V(DEBUG, TAG, "CASendMulticastMessageImpl, send to, data: %p, %u", data, dataLen);
789 VERIFY_NON_NULL(data, TAG, "data is null");
790 VERIFY_NON_NULL(env, TAG, "env is null");
794 OIC_LOG(ERROR, TAG, "g_deviceList is null");
795 return CA_STATUS_FAILED;
798 ca_mutex_lock(g_threadSendMutex);
800 CALEClientSetSendFinishFlag(false);
802 OIC_LOG(DEBUG, TAG, "set byteArray for data");
805 (*env)->DeleteGlobalRef(env, g_sendBuffer);
809 CAResult_t res = CALEClientIsThereScannedDevices();
810 if (CA_STATUS_OK != res)
812 OIC_LOG(INFO, TAG, "there is no scanned device");
816 // connect to gatt server
817 res = CALEClientStopScan();
818 if (CA_STATUS_OK != res)
820 OIC_LOG(ERROR, TAG, "CALEClientStopScan has failed");
821 ca_mutex_unlock(g_threadSendMutex);
824 uint32_t length = u_arraylist_length(g_deviceList);
825 g_targetCnt = length;
827 jbyteArray jni_arr = (*env)->NewByteArray(env, dataLen);
828 (*env)->SetByteArrayRegion(env, jni_arr, 0, dataLen, (jbyte*) data);
829 g_sendBuffer = (jbyteArray)(*env)->NewGlobalRef(env, jni_arr);
831 for (uint32_t index = 0; index < length; index++)
833 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
836 OIC_LOG(ERROR, TAG, "jarrayObj is not available");
840 res = CALEClientSendData(env, jarrayObj);
841 if (res != CA_STATUS_OK)
843 OIC_LOG(ERROR, TAG, "BT device - send has failed");
846 jstring jni_address = CALEGetAddressFromBTDevice(env, jarrayObj);
849 OIC_LOG(ERROR, TAG, "CALEGetAddressFromBTDevice has failed");
853 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
856 OIC_LOG(ERROR, TAG, "address is not available");
860 (*env)->ReleaseStringUTFChars(env, jni_address, address);
863 OIC_LOG(DEBUG, TAG, "connection routine is finished for multicast");
865 // wait for finish to send data through "CALeGattServicesDiscoveredCallback"
866 if (!g_isFinishedSendData)
868 ca_mutex_lock(g_threadMutex);
869 ca_cond_wait(g_threadCond, g_threadMutex);
870 OIC_LOG(DEBUG, TAG, "the data was sent for All devices");
871 ca_mutex_unlock(g_threadMutex);
874 // start LE Scan again
875 res = CALEClientStartScan();
876 if (CA_STATUS_OK != res)
878 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
879 ca_mutex_unlock(g_threadSendMutex);
883 ca_mutex_unlock(g_threadSendMutex);
884 OIC_LOG(DEBUG, TAG, "OUT - CALEClientSendMulticastMessageImpl");
888 res = CALEClientStartScan();
889 if (CA_STATUS_OK != res)
891 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
892 ca_mutex_unlock(g_threadSendMutex);
896 ca_mutex_unlock(g_threadSendMutex);
897 OIC_LOG(DEBUG, TAG, "OUT - CALEClientSendMulticastMessageImpl");
898 return CA_SEND_FAILED;
901 CAResult_t CALECheckSendState(const char* address)
903 VERIFY_NON_NULL(address, TAG, "address is null");
905 ca_mutex_lock(g_deviceStateListMutex);
906 CALEState_t* state = CALEClientGetStateInfo(address);
909 OIC_LOG(ERROR, TAG, "state is null");
910 ca_mutex_unlock(g_deviceStateListMutex);
911 return CA_SEND_FAILED;
914 if (STATE_SEND_SUCCESS != state->sendState)
916 OIC_LOG(ERROR, TAG, "sendstate is not STATE_SEND_SUCCESS");
917 ca_mutex_unlock(g_deviceStateListMutex);
918 return CA_SEND_FAILED;
920 ca_mutex_unlock(g_deviceStateListMutex);
924 CAResult_t CALEClientSendData(JNIEnv *env, jobject device)
926 OIC_LOG(DEBUG, TAG, "IN - CALEClientSendData");
927 VERIFY_NON_NULL(device, TAG, "device is null");
928 VERIFY_NON_NULL(env, TAG, "env is null");
930 jstring jni_address = CALEGetAddressFromBTDevice(env, device);
933 OIC_LOG(ERROR, TAG, "CALEGetAddressFromBTDevice has failed");
934 return CA_STATUS_FAILED;
937 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
940 OIC_LOG(ERROR, TAG, "address is not available");
941 return CA_STATUS_FAILED;
944 ca_mutex_lock(g_deviceStateListMutex);
945 CALEState_t* state = CALEClientGetStateInfo(address);
946 ca_mutex_unlock(g_deviceStateListMutex);
949 OIC_LOG(DEBUG, TAG, "state is empty..start to connect LE");
950 CAResult_t ret = CALEClientConnect(env, device, JNI_FALSE, g_leGattCallback);
951 if (CA_STATUS_OK != ret)
953 OIC_LOG(ERROR, TAG, "CALEClientConnect has failed");
954 (*env)->ReleaseStringUTFChars(env, jni_address, address);
960 if (STATE_CONNECTED == state->connectedState)
962 OIC_LOG(INFO, TAG, "GATT has already connected");
963 jobject gatt = CALEClientGetGattObjInList(env, address);
966 OIC_LOG(ERROR, TAG, "CALEClientGetGattObjInList has failed");
967 (*env)->ReleaseStringUTFChars(env, jni_address, address);
968 return CA_STATUS_FAILED;
971 CAResult_t ret = CALESetValueAndWriteCharacteristic(env, gatt);
972 if (CA_STATUS_OK != ret)
974 OIC_LOG(ERROR, TAG, "CALESetValueAndWriteCharacteristic has failed");
975 (*env)->ReleaseStringUTFChars(env, jni_address, address);
981 OIC_LOG(DEBUG, TAG, "start to connect LE");
982 CAResult_t ret = CALEClientConnect(env, device, JNI_FALSE, g_leGattCallback);
983 if (CA_STATUS_OK != ret)
985 OIC_LOG(ERROR, TAG, "CALEClientConnect has failed");
986 (*env)->ReleaseStringUTFChars(env, jni_address, address);
992 (*env)->ReleaseStringUTFChars(env, jni_address, address);
996 jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt)
998 VERIFY_NON_NULL_RET(gatt, TAG, "gatt is null", NULL);
999 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
1001 jclass jni_cid_gattdevice_list = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1002 if (!jni_cid_gattdevice_list)
1004 OIC_LOG(ERROR, TAG, "jni_cid_gattdevice_list is null");
1008 jmethodID jni_mid_getDevice = (*env)->GetMethodID(env, jni_cid_gattdevice_list, "getDevice",
1009 "()Landroid/bluetooth/BluetoothDevice;");
1010 if (!jni_mid_getDevice)
1012 OIC_LOG(ERROR, TAG, "jni_mid_getDevice is null");
1016 jobject jni_obj_device = (*env)->CallObjectMethod(env, gatt, jni_mid_getDevice);
1017 if (!jni_obj_device)
1019 OIC_LOG(ERROR, TAG, "jni_obj_device is null");
1023 jstring jni_address = CALEGetAddressFromBTDevice(env, jni_obj_device);
1026 OIC_LOG(ERROR, TAG, "jni_address is null");
1036 CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt)
1039 OIC_LOG(DEBUG, TAG, "Gatt Close");
1040 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1041 VERIFY_NON_NULL(env, TAG, "env is null");
1043 // get BluetoothGatt class
1044 OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
1045 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1046 if (!jni_cid_BluetoothGatt)
1048 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1049 return CA_STATUS_FAILED;
1052 jmethodID jni_mid_closeGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "close", "()V");
1053 if (!jni_mid_closeGatt)
1055 OIC_LOG(ERROR, TAG, "jni_mid_closeGatt is null");
1056 return CA_STATUS_OK;
1059 // call disconnect gatt method
1060 OIC_LOG(DEBUG, TAG, "request to close GATT");
1061 (*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_closeGatt);
1063 if ((*env)->ExceptionCheck(env))
1065 OIC_LOG(ERROR, TAG, "closeGATT has failed");
1066 (*env)->ExceptionDescribe(env);
1067 (*env)->ExceptionClear(env);
1068 return CA_STATUS_FAILED;
1071 return CA_STATUS_OK;
1074 CAResult_t CALEClientStartScan()
1076 if (!g_isStartedMulticastServer)
1078 OIC_LOG(ERROR, TAG, "server is not started yet..scan will be passed");
1079 return CA_STATUS_FAILED;
1082 if (!g_isStartedLEClient)
1084 OIC_LOG(ERROR, TAG, "LE client is not started");
1085 return CA_STATUS_FAILED;
1090 OIC_LOG(ERROR, TAG, "g_jvm is null");
1091 return CA_STATUS_FAILED;
1094 if (g_isStartedScan)
1096 OIC_LOG(INFO, TAG, "scanning is already started");
1097 return CA_STATUS_OK;
1100 bool isAttached = false;
1102 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1105 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
1107 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1110 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1111 return CA_STATUS_FAILED;
1116 OIC_LOG(DEBUG, TAG, "CALEClientStartScan");
1118 CAResult_t ret = CA_STATUS_OK;
1119 // scan gatt server with UUID
1120 if (g_leScanCallback && g_uuidList)
1123 ret = CALEClientStartScanWithUUIDImpl(env, g_uuidList, g_leScanCallback);
1124 if(CA_STATUS_OK != ret)
1126 OIC_LOG(ERROR, TAG, "CALEClientStartScanWithUUIDImpl has failed");
1129 ret = CALEClientStartScanImpl(env, g_leScanCallback);
1130 if (CA_STATUS_OK != ret)
1132 OIC_LOG(ERROR, TAG, "CALEClientStartScanImpl has failed");
1139 (*g_jvm)->DetachCurrentThread(g_jvm);
1145 CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback)
1147 VERIFY_NON_NULL(callback, TAG, "callback is null");
1148 VERIFY_NON_NULL(env, TAG, "env is null");
1150 if (!CALEIsEnableBTAdapter(env))
1152 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1153 return CA_ADAPTER_NOT_ENABLED;
1156 // get default bt adapter class
1157 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
1158 if (!jni_cid_BTAdapter)
1160 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_cid_BTAdapter is null");
1161 return CA_STATUS_FAILED;
1164 // get remote bt adapter method
1165 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
1166 "getDefaultAdapter",
1167 METHODID_OBJECTNONPARAM);
1168 if (!jni_mid_getDefaultAdapter)
1170 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
1171 return CA_STATUS_FAILED;
1174 // get start le scan method
1175 jmethodID jni_mid_startLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "startLeScan",
1176 "(Landroid/bluetooth/BluetoothAdapter$"
1177 "LeScanCallback;)Z");
1178 if (!jni_mid_startLeScan)
1180 OIC_LOG(ERROR, TAG, "startLeScan: jni_mid_startLeScan is null");
1181 return CA_STATUS_FAILED;
1184 // gat bt adapter object
1185 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
1186 jni_mid_getDefaultAdapter);
1187 if (!jni_obj_BTAdapter)
1189 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_obj_BTAdapter is null");
1190 return CA_STATUS_FAILED;
1193 // call start le scan method
1194 jboolean jni_obj_startLeScan = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter,
1195 jni_mid_startLeScan, callback);
1196 if (!jni_obj_startLeScan)
1198 OIC_LOG(ERROR, TAG, "startLeScan is failed");
1199 return CA_STATUS_FAILED;
1203 OIC_LOG(DEBUG, TAG, "startLeScan is started");
1204 CALEClientSetScanFlag(true);
1207 return CA_STATUS_OK;
1210 CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids, jobject callback)
1212 VERIFY_NON_NULL(callback, TAG, "callback is null");
1213 VERIFY_NON_NULL(uuids, TAG, "uuids is null");
1214 VERIFY_NON_NULL(env, TAG, "env is null");
1216 if (!CALEIsEnableBTAdapter(env))
1218 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1219 return CA_ADAPTER_NOT_ENABLED;
1222 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
1223 if (!jni_cid_BTAdapter)
1225 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_cid_BTAdapter is null");
1226 return CA_STATUS_FAILED;
1229 // get remote bt adapter method
1230 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
1231 "getDefaultAdapter",
1232 METHODID_OBJECTNONPARAM);
1233 if (!jni_mid_getDefaultAdapter)
1235 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
1236 return CA_STATUS_FAILED;
1239 // get start le scan method
1240 jmethodID jni_mid_startLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "startLeScan",
1241 "([Ljava/util/UUID;Landroid/bluetooth/"
1242 "BluetoothAdapter$LeScanCallback;)Z");
1243 if (!jni_mid_startLeScan)
1245 OIC_LOG(ERROR, TAG, "startLeScan: jni_mid_startLeScan is null");
1246 return CA_STATUS_FAILED;
1249 // get bt adapter object
1250 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
1251 jni_mid_getDefaultAdapter);
1252 if (!jni_obj_BTAdapter)
1254 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_obj_BTAdapter is null");
1255 return CA_STATUS_FAILED;
1258 // call start le scan method
1259 jboolean jni_obj_startLeScan = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter,
1260 jni_mid_startLeScan, uuids, callback);
1261 if (!jni_obj_startLeScan)
1263 OIC_LOG(ERROR, TAG, "startLeScan With UUID is failed");
1264 return CA_STATUS_FAILED;
1268 OIC_LOG(DEBUG, TAG, "startLeScan With UUID is started");
1269 CALEClientSetScanFlag(true);
1272 return CA_STATUS_OK;
1275 jobject CALEClientGetUUIDObject(JNIEnv *env, const char* uuid)
1277 VERIFY_NON_NULL_RET(uuid, TAG, "uuid is null", NULL);
1278 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
1281 jclass jni_cid_uuid = (*env)->FindClass(env, CLASSPATH_BT_UUID);
1284 OIC_LOG(ERROR, TAG, "jni_cid_uuid is null");
1288 jmethodID jni_mid_fromString = (*env)->GetStaticMethodID(env, jni_cid_uuid, "fromString",
1289 "(Ljava/lang/String;)"
1290 "Ljava/util/UUID;");
1291 if (!jni_mid_fromString)
1293 OIC_LOG(ERROR, TAG, "jni_mid_fromString is null");
1297 jstring jni_uuid = (*env)->NewStringUTF(env, uuid);
1298 jobject jni_obj_uuid = (*env)->CallStaticObjectMethod(env, jni_cid_uuid, jni_mid_fromString,
1302 OIC_LOG(ERROR, TAG, "jni_obj_uuid is null");
1306 return jni_obj_uuid;
1309 CAResult_t CALEClientStopScan()
1313 OIC_LOG(ERROR, TAG, "g_jvm is null");
1314 return CA_STATUS_FAILED;
1317 if (!g_isStartedScan)
1319 OIC_LOG(INFO, TAG, "scanning is already stopped");
1320 return CA_STATUS_OK;
1323 bool isAttached = false;
1325 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1328 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
1329 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1332 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1333 return CA_STATUS_FAILED;
1338 CAResult_t ret = CALEClientStopScanImpl(env, g_leScanCallback);
1339 if (CA_STATUS_OK != ret)
1341 OIC_LOG(ERROR, TAG, "CALEClientStopScanImpl has failed");
1345 CALEClientSetScanFlag(false);
1350 (*g_jvm)->DetachCurrentThread(g_jvm);
1356 void CALEClientSetScanFlag(bool flag)
1358 ca_mutex_lock(g_scanMutex);
1359 g_isStartedScan = flag;
1360 ca_mutex_unlock(g_scanMutex);
1363 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback)
1365 OIC_LOG(DEBUG, TAG, "CALEClientStopScanImpl");
1366 VERIFY_NON_NULL(callback, TAG, "callback is null");
1367 VERIFY_NON_NULL(env, TAG, "env is null");
1369 if (!CALEIsEnableBTAdapter(env))
1371 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1372 return CA_ADAPTER_NOT_ENABLED;
1375 // get default bt adapter class
1376 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
1377 if (!jni_cid_BTAdapter)
1379 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_cid_BTAdapter is null");
1380 return CA_STATUS_FAILED;
1383 // get remote bt adapter method
1384 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
1385 "getDefaultAdapter",
1386 METHODID_OBJECTNONPARAM);
1387 if (!jni_mid_getDefaultAdapter)
1389 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
1390 return CA_STATUS_FAILED;
1393 // get start le scan method
1394 jmethodID jni_mid_stopLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "stopLeScan",
1395 "(Landroid/bluetooth/"
1396 "BluetoothAdapter$LeScanCallback;)V");
1397 if (!jni_mid_stopLeScan)
1399 OIC_LOG(ERROR, TAG, "stopLeScan: jni_mid_stopLeScan is null");
1400 return CA_STATUS_FAILED;
1403 // gat bt adapter object
1404 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
1405 jni_mid_getDefaultAdapter);
1406 if (!jni_obj_BTAdapter)
1408 OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
1409 return CA_STATUS_FAILED;
1412 OIC_LOG(DEBUG, TAG, "CALL API - request to stop LE Scan");
1413 // call start le scan method
1414 (*env)->CallVoidMethod(env, jni_obj_BTAdapter, jni_mid_stopLeScan, callback);
1415 if ((*env)->ExceptionCheck(env))
1417 OIC_LOG(ERROR, TAG, "stopLeScan has failed");
1418 (*env)->ExceptionDescribe(env);
1419 (*env)->ExceptionClear(env);
1420 return CA_STATUS_FAILED;
1423 return CA_STATUS_OK;
1426 CAResult_t CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect,
1429 OIC_LOG(DEBUG, TAG, "GATT CONNECT");
1430 VERIFY_NON_NULL(env, TAG, "env is null");
1431 VERIFY_NON_NULL(bluetoothDevice, TAG, "bluetoothDevice is null");
1432 VERIFY_NON_NULL(callback, TAG, "callback is null");
1434 if (!CALEIsEnableBTAdapter(env))
1436 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1437 return CA_ADAPTER_NOT_ENABLED;
1440 jstring jni_address = CALEGetAddressFromBTDevice(env, bluetoothDevice);
1443 OIC_LOG(ERROR, TAG, "bleConnect: CALEGetAddressFromBTDevice is null");
1444 return CA_STATUS_FAILED;
1447 // get BluetoothDevice class
1448 OIC_LOG(DEBUG, TAG, "get BluetoothDevice class");
1449 jclass jni_cid_BluetoothDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
1450 if (!jni_cid_BluetoothDevice)
1452 OIC_LOG(ERROR, TAG, "bleConnect: jni_cid_BluetoothDevice is null");
1453 return CA_STATUS_FAILED;
1456 // get connectGatt method
1457 OIC_LOG(DEBUG, TAG, "get connectGatt method");
1458 jmethodID jni_mid_connectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothDevice, "connectGatt",
1459 "(Landroid/content/Context;ZLandroid/"
1460 "bluetooth/BluetoothGattCallback;)"
1461 "Landroid/bluetooth/BluetoothGatt;");
1462 if (!jni_mid_connectGatt)
1464 OIC_LOG(ERROR, TAG, "bleConnect: jni_mid_connectGatt is null");
1465 return CA_STATUS_FAILED;
1468 OIC_LOG(DEBUG, TAG, "Call object method - connectGatt");
1469 jobject jni_obj_connectGatt = (*env)->CallObjectMethod(env, bluetoothDevice,
1470 jni_mid_connectGatt,
1472 autoconnect, callback);
1473 if (!jni_obj_connectGatt)
1475 OIC_LOG(ERROR, TAG, "CALL API - connectGatt was failed..it will be removed");
1476 CALEClientRemoveDeviceInScanDeviceList(env, jni_address);
1477 CALEClientUpdateSendCnt(env);
1478 return CA_STATUS_FAILED;
1482 OIC_LOG(DEBUG, TAG, "le connecting..please wait..");
1484 return CA_STATUS_OK;
1487 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt)
1489 OIC_LOG(DEBUG, TAG, "GATT DISCONNECT");
1490 VERIFY_NON_NULL(env, TAG, "env is null");
1491 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1493 if (!CALEIsEnableBTAdapter(env))
1495 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1496 return CA_ADAPTER_NOT_ENABLED;
1499 // get BluetoothGatt class
1500 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1501 if (!jni_cid_BluetoothGatt)
1503 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1504 return CA_STATUS_FAILED;
1507 OIC_LOG(DEBUG, TAG, "get gatt disconnect method");
1508 jmethodID jni_mid_disconnectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1509 "disconnect", "()V");
1510 if (!jni_mid_disconnectGatt)
1512 OIC_LOG(ERROR, TAG, "jni_mid_disconnectGatt is null");
1513 return CA_STATUS_FAILED;
1516 // call disconnect gatt method
1517 (*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_disconnectGatt);
1518 if ((*env)->ExceptionCheck(env))
1520 OIC_LOG(ERROR, TAG, "disconnect has failed");
1521 (*env)->ExceptionDescribe(env);
1522 (*env)->ExceptionClear(env);
1523 return CA_STATUS_FAILED;
1526 OIC_LOG(DEBUG, TAG, "disconnecting Gatt...");
1528 return CA_STATUS_OK;
1531 CAResult_t CALEClientDisconnectAll(JNIEnv *env)
1533 OIC_LOG(DEBUG, TAG, "CALEClientDisconnectAll");
1534 VERIFY_NON_NULL(env, TAG, "env is null");
1536 if (!g_gattObjectList)
1538 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
1539 return CA_STATUS_FAILED;
1542 uint32_t length = u_arraylist_length(g_gattObjectList);
1543 for (uint32_t index = 0; index < length; index++)
1545 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
1548 OIC_LOG(ERROR, TAG, "jarrayObj is null");
1551 CAResult_t res = CALEClientDisconnect(env, jarrayObj);
1552 if (CA_STATUS_OK != res)
1554 OIC_LOG(ERROR, TAG, "CALEClientDisconnect has failed");
1559 OICFree(g_gattObjectList);
1560 g_gattObjectList = NULL;
1562 return CA_STATUS_OK;
1565 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt)
1567 VERIFY_NON_NULL(env, TAG, "env is null");
1568 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1570 if (!CALEIsEnableBTAdapter(env))
1572 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1573 return CA_ADAPTER_NOT_ENABLED;
1576 // get BluetoothGatt class
1577 OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
1578 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1579 if (!jni_cid_BluetoothGatt)
1581 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1582 return CA_STATUS_FAILED;
1585 OIC_LOG(DEBUG, TAG, "discovery gatt services method");
1586 jmethodID jni_mid_discoverServices = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1587 "discoverServices", "()Z");
1588 if (!jni_mid_discoverServices)
1590 OIC_LOG(ERROR, TAG, "jni_mid_discoverServices is null");
1591 return CA_STATUS_FAILED;
1593 // call disconnect gatt method
1594 OIC_LOG(DEBUG, TAG, "CALL API - request discovery gatt services");
1595 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_discoverServices);
1598 OIC_LOG(ERROR, TAG, "discoverServices has not been started");
1599 return CA_STATUS_FAILED;
1602 return CA_STATUS_OK;
1605 static void CALEWriteCharacteristicThread(void* object)
1607 VERIFY_NON_NULL(object, TAG, "object is null");
1609 bool isAttached = false;
1611 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1614 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
1615 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1619 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1625 jobject gatt = (jobject)object;
1626 CAResult_t ret = CALESetValueAndWriteCharacteristic(env, gatt);
1627 if (CA_STATUS_OK != ret)
1629 OIC_LOG(ERROR, TAG, "CALESetValueAndWriteCharacteristic has failed");
1634 (*g_jvm)->DetachCurrentThread(g_jvm);
1638 CAResult_t CALESetValueAndWriteCharacteristic(JNIEnv* env, jobject gatt)
1640 VERIFY_NON_NULL(gatt, TAG, "gatt is null");
1641 VERIFY_NON_NULL(env, TAG, "env is null");
1644 jobject jni_obj_character = CALEClientCreateGattCharacteristic(env, gatt, g_sendBuffer);
1645 if (!jni_obj_character)
1647 CALEClientSendFinish(env, gatt);
1648 return CA_STATUS_FAILED;
1651 CAResult_t ret = CALEClientWriteCharacteristicImpl(env, gatt, jni_obj_character);
1652 if (CA_STATUS_OK != ret)
1654 CALEClientSendFinish(env, gatt);
1655 return CA_STATUS_FAILED;
1658 // wait for callback for write Characteristic with success to sent data
1659 OIC_LOG_V(DEBUG, TAG, "callback flag is %d", g_isSignalSetFlag);
1660 ca_mutex_lock(g_threadWriteCharacteristicMutex);
1661 if (!g_isSignalSetFlag)
1663 OIC_LOG(DEBUG, TAG, "wait for callback to notify writeCharacteristic is success");
1664 if (CA_WAIT_SUCCESS != ca_cond_wait_for(g_threadWriteCharacteristicCond,
1665 g_threadWriteCharacteristicMutex,
1666 WAIT_TIME_WRITE_CHARACTERISTIC))
1668 OIC_LOG(ERROR, TAG, "there is no response. write has failed");
1669 g_isSignalSetFlag = false;
1670 ca_mutex_unlock(g_threadWriteCharacteristicMutex);
1671 return CA_STATUS_FAILED;
1674 // reset flag set by writeCharacteristic Callback
1675 g_isSignalSetFlag = false;
1676 ca_mutex_unlock(g_threadWriteCharacteristicMutex);
1678 OIC_LOG(INFO, TAG, "writeCharacteristic success!!");
1679 return CA_STATUS_OK;
1682 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt)
1684 OIC_LOG(DEBUG, TAG, "IN - CALEClientWriteCharacteristic");
1685 VERIFY_NON_NULL(env, TAG, "env is null");
1686 VERIFY_NON_NULL(gatt, TAG, "gatt is null");
1688 jobject gattParam = (*env)->NewGlobalRef(env, gatt);
1689 if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle,
1690 CALEWriteCharacteristicThread, (void*)gattParam))
1692 OIC_LOG(ERROR, TAG, "Failed to create read thread!");
1693 return CA_STATUS_FAILED;
1696 OIC_LOG(DEBUG, TAG, "OUT - CALEClientWriteCharacteristic");
1697 return CA_STATUS_OK;
1700 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
1701 jobject gattCharacteristic)
1703 OIC_LOG(DEBUG, TAG, "WRITE GATT CHARACTERISTIC");
1704 VERIFY_NON_NULL(env, TAG, "env is null");
1705 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1706 VERIFY_NON_NULL(gattCharacteristic, TAG, "gattCharacteristic is null");
1708 if (!CALEIsEnableBTAdapter(env))
1710 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1711 return CA_STATUS_FAILED;
1714 // get BluetoothGatt class
1715 OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
1716 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1717 if (!jni_cid_BluetoothGatt)
1719 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1720 return CA_STATUS_FAILED;
1723 OIC_LOG(DEBUG, TAG, "write characteristic method");
1724 jmethodID jni_mid_writeCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1725 "writeCharacteristic",
1726 "(Landroid/bluetooth/"
1727 "BluetoothGattCharacteristic;)Z");
1728 if (!jni_mid_writeCharacteristic)
1730 OIC_LOG(ERROR, TAG, "jni_mid_writeCharacteristic is null");
1731 return CA_STATUS_FAILED;
1734 // call disconnect gatt method
1735 OIC_LOG(DEBUG, TAG, "CALL API - request to write gatt characteristic");
1736 jboolean ret = (jboolean)(*env)->CallBooleanMethod(env, bluetoothGatt,
1737 jni_mid_writeCharacteristic,
1738 gattCharacteristic);
1741 OIC_LOG(DEBUG, TAG, "writeCharacteristic is called successfully");
1745 OIC_LOG(ERROR, TAG, "writeCharacteristic has failed");
1746 return CA_STATUS_FAILED;
1749 return CA_STATUS_OK;
1752 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt)
1754 VERIFY_NON_NULL(env, TAG, "env is null");
1755 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1757 if (!CALEIsEnableBTAdapter(env))
1759 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1760 return CA_STATUS_FAILED;
1763 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1764 if (!jni_cid_BluetoothGatt)
1766 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1767 return CA_STATUS_FAILED;
1770 jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_RESPONSE_UUID);
1773 OIC_LOG(ERROR, TAG, "jni_uuid is null");
1774 return CA_STATUS_FAILED;
1777 jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, bluetoothGatt, jni_uuid);
1778 if (!jni_obj_GattCharacteristic)
1780 OIC_LOG(ERROR, TAG, "jni_obj_GattCharacteristic is null");
1781 return CA_STATUS_FAILED;
1784 OIC_LOG(DEBUG, TAG, "read characteristic method");
1785 jmethodID jni_mid_readCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1786 "readCharacteristic",
1787 "(Landroid/bluetooth/"
1788 "BluetoothGattCharacteristic;)Z");
1789 if (!jni_mid_readCharacteristic)
1791 OIC_LOG(ERROR, TAG, "jni_mid_readCharacteristic is null");
1792 return CA_STATUS_FAILED;
1795 // call disconnect gatt method
1796 OIC_LOG(DEBUG, TAG, "CALL API - request to read gatt characteristic");
1797 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_readCharacteristic,
1798 jni_obj_GattCharacteristic);
1801 OIC_LOG(DEBUG, TAG, "readCharacteristic success");
1805 OIC_LOG(ERROR, TAG, "readCharacteristic has failed");
1806 return CA_STATUS_FAILED;
1809 return CA_STATUS_OK;
1812 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
1813 jobject characteristic)
1815 VERIFY_NON_NULL(env, TAG, "env is null");
1816 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1817 VERIFY_NON_NULL(characteristic, TAG, "characteristic is null");
1819 if (!CALEIsEnableBTAdapter(env))
1821 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1822 return CA_ADAPTER_NOT_ENABLED;
1825 // get BluetoothGatt class
1826 OIC_LOG(DEBUG, TAG, "CALEClientSetCharacteristicNotification");
1827 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1828 if (!jni_cid_BluetoothGatt)
1830 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1831 return CA_STATUS_FAILED;
1834 // set Characteristic Notification
1835 jmethodID jni_mid_setNotification = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1836 "setCharacteristicNotification",
1837 "(Landroid/bluetooth/"
1838 "BluetoothGattCharacteristic;Z)Z");
1839 if (!jni_mid_setNotification)
1841 OIC_LOG(ERROR, TAG, "jni_mid_getService is null");
1842 return CA_STATUS_FAILED;
1845 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_setNotification,
1846 characteristic, JNI_TRUE);
1847 if (JNI_TRUE == ret)
1849 OIC_LOG(DEBUG, TAG, "CALL API - setCharacteristicNotification success");
1853 OIC_LOG(ERROR, TAG, "CALL API - setCharacteristicNotification has failed");
1854 return CA_STATUS_FAILED;
1857 return CA_STATUS_OK;
1860 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID)
1862 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
1863 VERIFY_NON_NULL_RET(bluetoothGatt, TAG, "bluetoothGatt is null", NULL);
1864 VERIFY_NON_NULL_RET(characterUUID, TAG, "characterUUID is null", NULL);
1866 if (!CALEIsEnableBTAdapter(env))
1868 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1872 // get BluetoothGatt class
1873 OIC_LOG(DEBUG, TAG, "CALEClientGetGattService");
1874 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1875 if (!jni_cid_BluetoothGatt)
1877 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1881 jmethodID jni_mid_getService = (*env)->GetMethodID(
1882 env, jni_cid_BluetoothGatt, "getService",
1883 "(Ljava/util/UUID;)Landroid/bluetooth/BluetoothGattService;");
1884 if (!jni_mid_getService)
1886 OIC_LOG(ERROR, TAG, "jni_mid_getService is null");
1890 jobject jni_obj_service_uuid = CALEClientGetUUIDObject(env, OIC_GATT_SERVICE_UUID);
1891 if (!jni_obj_service_uuid)
1893 OIC_LOG(ERROR, TAG, "jni_obj_service_uuid is null");
1897 // get bluetooth gatt service
1898 OIC_LOG(DEBUG, TAG, "request to get service");
1899 jobject jni_obj_gattService = (*env)->CallObjectMethod(env, bluetoothGatt, jni_mid_getService,
1900 jni_obj_service_uuid);
1901 if (!jni_obj_gattService)
1903 OIC_LOG(ERROR, TAG, "jni_obj_gattService is null");
1907 // get bluetooth gatt service class
1908 jclass jni_cid_BluetoothGattService = (*env)->FindClass(
1909 env, "android/bluetooth/BluetoothGattService");
1910 if (!jni_cid_BluetoothGattService)
1912 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGattService is null");
1916 OIC_LOG(DEBUG, TAG, "get gatt getCharacteristic method");
1917 jmethodID jni_mid_getCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGattService,
1918 "getCharacteristic",
1919 "(Ljava/util/UUID;)"
1920 "Landroid/bluetooth/"
1921 "BluetoothGattCharacteristic;");
1922 if (!jni_mid_getCharacteristic)
1924 OIC_LOG(ERROR, TAG, "jni_mid_getCharacteristic is null");
1928 const char* uuid = (*env)->GetStringUTFChars(env, characterUUID, NULL);
1931 OIC_LOG(ERROR, TAG, "uuid is null");
1935 jobject jni_obj_tx_uuid = CALEClientGetUUIDObject(env, uuid);
1936 if (!jni_obj_tx_uuid)
1938 OIC_LOG(ERROR, TAG, "jni_obj_tx_uuid is null");
1939 (*env)->ReleaseStringUTFChars(env, characterUUID, uuid);
1943 OIC_LOG(DEBUG, TAG, "request to get Characteristic");
1944 jobject jni_obj_GattCharacteristic = (*env)->CallObjectMethod(env, jni_obj_gattService,
1945 jni_mid_getCharacteristic,
1948 (*env)->ReleaseStringUTFChars(env, characterUUID, uuid);
1949 return jni_obj_GattCharacteristic;
1952 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data)
1954 OIC_LOG(DEBUG, TAG, "CALEClientCreateGattCharacteristic");
1955 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
1956 VERIFY_NON_NULL_RET(bluetoothGatt, TAG, "bluetoothGatt is null", NULL);
1957 VERIFY_NON_NULL_RET(data, TAG, "data is null", NULL);
1959 if (!CALEIsEnableBTAdapter(env))
1961 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1965 jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_REQUEST_UUID);
1968 OIC_LOG(ERROR, TAG, "jni_uuid is null");
1972 jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, bluetoothGatt, jni_uuid);
1973 if (!jni_obj_GattCharacteristic)
1975 OIC_LOG(ERROR, TAG, "jni_obj_GattCharacteristic is null");
1979 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth"
1980 "/BluetoothGattCharacteristic");
1981 if (!jni_cid_BTGattCharacteristic)
1983 OIC_LOG(ERROR, TAG, "jni_cid_BTGattCharacteristic is null");
1987 OIC_LOG(DEBUG, TAG, "set value in Characteristic");
1988 jmethodID jni_mid_setValue = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, "setValue",
1990 if (!jni_mid_setValue)
1992 OIC_LOG(ERROR, TAG, "jni_mid_setValue is null");
1996 jboolean ret = (*env)->CallBooleanMethod(env, jni_obj_GattCharacteristic, jni_mid_setValue,
1998 if (JNI_TRUE == ret)
2000 OIC_LOG(DEBUG, TAG, "the locally stored value has been set");
2004 OIC_LOG(ERROR, TAG, "the locally stored value hasn't been set");
2009 jmethodID jni_mid_setWriteType = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic,
2010 "setWriteType", "(I)V");
2011 if (!jni_mid_setWriteType)
2013 OIC_LOG(ERROR, TAG, "jni_mid_setWriteType is null");
2017 jfieldID jni_fid_no_response = (*env)->GetStaticFieldID(env, jni_cid_BTGattCharacteristic,
2018 "WRITE_TYPE_NO_RESPONSE", "I");
2019 if (!jni_fid_no_response)
2021 OIC_LOG(ERROR, TAG, "jni_fid_no_response is not available");
2025 jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTGattCharacteristic,
2026 jni_fid_no_response);
2028 (*env)->CallVoidMethod(env, jni_obj_GattCharacteristic, jni_mid_setWriteType, jni_int_val);
2030 return jni_obj_GattCharacteristic;
2033 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic)
2035 VERIFY_NON_NULL_RET(characteristic, TAG, "characteristic is null", NULL);
2036 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
2038 if (!CALEIsEnableBTAdapter(env))
2040 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
2044 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/"
2045 "BluetoothGattCharacteristic");
2046 if (!jni_cid_BTGattCharacteristic)
2048 OIC_LOG(ERROR, TAG, "jni_cid_BTGattCharacteristic is null");
2052 OIC_LOG(DEBUG, TAG, "get value in Characteristic");
2053 jmethodID jni_mid_getValue = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, "getValue",
2055 if (!jni_mid_getValue)
2057 OIC_LOG(ERROR, TAG, "jni_mid_getValue is null");
2061 jbyteArray jni_obj_data_array = (*env)->CallObjectMethod(env, characteristic,
2063 return jni_obj_data_array;
2066 CAResult_t CALEClientCreateUUIDList()
2070 OIC_LOG(ERROR, TAG, "g_jvm is null");
2071 return CA_STATUS_FAILED;
2074 bool isAttached = false;
2076 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
2079 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
2080 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
2084 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
2085 return CA_STATUS_FAILED;
2090 // create new object array
2091 jclass jni_cid_uuid_list = (*env)->FindClass(env, CLASSPATH_BT_UUID);
2092 if (!jni_cid_uuid_list)
2094 OIC_LOG(ERROR, TAG, "jni_cid_uuid_list is null");
2098 jobjectArray jni_obj_uuid_list = (jobjectArray)(*env)->NewObjectArray(env, 1,
2099 jni_cid_uuid_list, NULL);
2100 if (!jni_obj_uuid_list)
2102 OIC_LOG(ERROR, TAG, "jni_obj_uuid_list is null");
2107 jobject jni_obj_uuid = CALEClientGetUUIDObject(env, OIC_GATT_SERVICE_UUID);
2110 OIC_LOG(ERROR, TAG, "jni_obj_uuid is null");
2113 (*env)->SetObjectArrayElement(env, jni_obj_uuid_list, 0, jni_obj_uuid);
2115 g_uuidList = (jobjectArray)(*env)->NewGlobalRef(env, jni_obj_uuid_list);
2119 (*g_jvm)->DetachCurrentThread(g_jvm);
2122 return CA_STATUS_OK;
2129 (*g_jvm)->DetachCurrentThread(g_jvm);
2131 return CA_STATUS_FAILED;
2134 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
2135 jobject characteristic)
2137 VERIFY_NON_NULL(env, TAG, "env is null");
2138 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
2139 VERIFY_NON_NULL(characteristic, TAG, "characteristic is null");
2141 if (!CALEIsEnableBTAdapter(env))
2143 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
2144 return CA_ADAPTER_NOT_ENABLED;
2147 OIC_LOG(DEBUG, TAG, "CALEClientSetUUIDToDescriptor");
2148 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/"
2149 "BluetoothGattCharacteristic");
2150 if (!jni_cid_BTGattCharacteristic)
2152 OIC_LOG(ERROR, TAG, "jni_cid_BTGattCharacteristic is null");
2153 return CA_STATUS_FAILED;
2156 OIC_LOG(DEBUG, TAG, "set value in Characteristic");
2157 jmethodID jni_mid_getDescriptor = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic,
2159 "(Ljava/util/UUID;)Landroid/bluetooth/"
2160 "BluetoothGattDescriptor;");
2161 if (!jni_mid_getDescriptor)
2163 OIC_LOG(ERROR, TAG, "jni_mid_getDescriptor is null");
2164 return CA_STATUS_FAILED;
2167 jobject jni_obj_cc_uuid = CALEClientGetUUIDObject(env, OIC_GATT_CHARACTERISTIC_CONFIG_UUID);
2168 if (!jni_obj_cc_uuid)
2170 OIC_LOG(ERROR, TAG, "jni_obj_cc_uuid is null");
2171 return CA_STATUS_FAILED;
2174 OIC_LOG(DEBUG, TAG, "request to get descriptor");
2175 jobject jni_obj_descriptor = (*env)->CallObjectMethod(env, characteristic,
2176 jni_mid_getDescriptor, jni_obj_cc_uuid);
2177 if (!jni_obj_descriptor)
2179 OIC_LOG(INFO, TAG, "jni_obj_descriptor is null");
2180 return CA_NOT_SUPPORTED;
2183 OIC_LOG(DEBUG, TAG, "set value in descriptor");
2184 jclass jni_cid_descriptor = (*env)->FindClass(env,
2185 "android/bluetooth/BluetoothGattDescriptor");
2186 if (!jni_cid_descriptor)
2188 OIC_LOG(ERROR, TAG, "jni_cid_descriptor is null");
2189 return CA_STATUS_FAILED;
2192 jmethodID jni_mid_setValue = (*env)->GetMethodID(env, jni_cid_descriptor, "setValue", "([B)Z");
2193 if (!jni_mid_setValue)
2195 OIC_LOG(ERROR, TAG, "jni_mid_setValue is null");
2196 return CA_STATUS_FAILED;
2199 jfieldID jni_fid_NotiValue = (*env)->GetStaticFieldID(env, jni_cid_descriptor,
2200 "ENABLE_NOTIFICATION_VALUE", "[B");
2201 if (!jni_fid_NotiValue)
2203 OIC_LOG(ERROR, TAG, "jni_fid_NotiValue is null");
2204 return CA_STATUS_FAILED;
2207 OIC_LOG(DEBUG, TAG, "get ENABLE_NOTIFICATION_VALUE");
2209 jboolean jni_setvalue = (*env)->CallBooleanMethod(
2210 env, jni_obj_descriptor, jni_mid_setValue,
2211 (jbyteArray)(*env)->GetStaticObjectField(env, jni_cid_descriptor, jni_fid_NotiValue));
2214 OIC_LOG(DEBUG, TAG, "setValue success");
2218 OIC_LOG(ERROR, TAG, "setValue has failed");
2219 return CA_STATUS_FAILED;
2222 jclass jni_cid_gatt = (*env)->FindClass(env, "android/bluetooth/BluetoothGatt");
2225 OIC_LOG(ERROR, TAG, "jni_cid_gatt is null");
2226 return CA_STATUS_FAILED;
2229 OIC_LOG(DEBUG, TAG, "write Descriptor in gatt object");
2230 jmethodID jni_mid_writeDescriptor = (*env)->GetMethodID(env, jni_cid_gatt, "writeDescriptor",
2231 "(Landroid/bluetooth/"
2232 "BluetoothGattDescriptor;)Z");
2233 if (!jni_mid_writeDescriptor)
2235 OIC_LOG(ERROR, TAG, "jni_mid_writeDescriptor is null");
2236 return CA_STATUS_FAILED;
2239 OIC_LOG(DEBUG, TAG, "request to write descriptor");
2240 jboolean jni_ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_writeDescriptor,
2241 jni_obj_descriptor);
2244 OIC_LOG(DEBUG, TAG, "writeDescriptor success");
2248 OIC_LOG(ERROR, TAG, "writeDescriptor has failed");
2249 return CA_STATUS_FAILED;
2252 return CA_STATUS_OK;
2255 void CALEClientCreateScanDeviceList(JNIEnv *env)
2257 OIC_LOG(DEBUG, TAG, "CALEClientCreateScanDeviceList");
2258 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
2260 ca_mutex_lock(g_deviceListMutex);
2261 // create new object array
2262 if (g_deviceList == NULL)
2264 OIC_LOG(DEBUG, TAG, "Create device list");
2266 g_deviceList = u_arraylist_create();
2268 ca_mutex_unlock(g_deviceListMutex);
2271 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device)
2273 VERIFY_NON_NULL(device, TAG, "device is null");
2274 VERIFY_NON_NULL(env, TAG, "env is null");
2276 ca_mutex_lock(g_deviceListMutex);
2280 OIC_LOG(ERROR, TAG, "gdevice_list is null");
2281 ca_mutex_unlock(g_deviceListMutex);
2282 return CA_STATUS_FAILED;
2285 jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
2286 if (!jni_remoteAddress)
2288 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
2289 ca_mutex_unlock(g_deviceListMutex);
2290 return CA_STATUS_FAILED;
2293 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2296 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2297 ca_mutex_unlock(g_deviceListMutex);
2298 return CA_STATUS_FAILED;
2301 if (!CALEClientIsDeviceInScanDeviceList(env, remoteAddress))
2303 jobject gdevice = (*env)->NewGlobalRef(env, device);
2304 u_arraylist_add(g_deviceList, gdevice);
2305 ca_cond_signal(g_deviceDescCond);
2306 OIC_LOG_V(DEBUG, TAG, "Added this Device[%s] in the List", remoteAddress);
2308 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2310 ca_mutex_unlock(g_deviceListMutex);
2312 return CA_STATUS_OK;
2315 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char* remoteAddress)
2317 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
2318 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", true);
2322 OIC_LOG(DEBUG, TAG, "g_deviceList is null");
2326 uint32_t length = u_arraylist_length(g_deviceList);
2327 for (uint32_t index = 0; index < length; index++)
2329 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
2332 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2336 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
2337 if (!jni_setAddress)
2339 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2343 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2346 OIC_LOG(ERROR, TAG, "setAddress is null");
2350 if (!strcmp(remoteAddress, setAddress))
2352 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2356 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2359 OIC_LOG(DEBUG, TAG, "there are no the device in list. we can add");
2364 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env)
2366 OIC_LOG(DEBUG, TAG, "CALEClientRemoveAllScanDevices");
2367 VERIFY_NON_NULL(env, TAG, "env is null");
2369 ca_mutex_lock(g_deviceListMutex);
2373 OIC_LOG(ERROR, TAG, "g_deviceList is null");
2374 ca_mutex_unlock(g_deviceListMutex);
2375 return CA_STATUS_FAILED;
2378 uint32_t length = u_arraylist_length(g_deviceList);
2379 for (uint32_t index = 0; index < length; index++)
2381 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
2384 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2387 (*env)->DeleteGlobalRef(env, jarrayObj);
2390 OICFree(g_deviceList);
2391 g_deviceList = NULL;
2393 ca_mutex_unlock(g_deviceListMutex);
2394 return CA_STATUS_OK;
2397 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring address)
2399 OIC_LOG(DEBUG, TAG, "CALEClientRemoveDeviceInScanDeviceList");
2400 VERIFY_NON_NULL(address, TAG, "address is null");
2401 VERIFY_NON_NULL(env, TAG, "env is null");
2403 ca_mutex_lock(g_deviceListMutex);
2407 OIC_LOG(ERROR, TAG, "g_deviceList is null");
2408 ca_mutex_unlock(g_deviceListMutex);
2409 return CA_STATUS_FAILED;
2412 uint32_t length = u_arraylist_length(g_deviceList);
2413 for (uint32_t index = 0; index < length; index++)
2415 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
2418 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2419 ca_mutex_unlock(g_deviceListMutex);
2420 return CA_STATUS_FAILED;
2423 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
2424 if (!jni_setAddress)
2426 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2427 ca_mutex_unlock(g_deviceListMutex);
2428 return CA_STATUS_FAILED;
2431 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2434 OIC_LOG(ERROR, TAG, "setAddress is null");
2435 ca_mutex_unlock(g_deviceListMutex);
2436 return CA_STATUS_FAILED;
2439 const char* remoteAddress = (*env)->GetStringUTFChars(env, address, NULL);
2442 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2443 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2444 ca_mutex_unlock(g_deviceListMutex);
2445 return CA_STATUS_FAILED;
2448 if (!strcmp(setAddress, remoteAddress))
2450 OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
2451 (*env)->DeleteGlobalRef(env, jarrayObj);
2452 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2453 (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
2455 if (NULL == u_arraylist_remove(g_deviceList, index))
2457 OIC_LOG(ERROR, TAG, "List removal failed.");
2458 ca_mutex_unlock(g_deviceListMutex);
2459 return CA_STATUS_FAILED;
2461 ca_mutex_unlock(g_deviceListMutex);
2462 return CA_STATUS_OK;
2464 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2465 (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
2468 ca_mutex_unlock(g_deviceListMutex);
2469 OIC_LOG(DEBUG, TAG, "There are no object in the device list");
2471 return CA_STATUS_OK;
2478 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt)
2480 OIC_LOG(DEBUG, TAG, "CALEClientAddGattobjToList");
2481 VERIFY_NON_NULL(env, TAG, "env is null");
2482 VERIFY_NON_NULL(gatt, TAG, "gatt is null");
2484 ca_mutex_lock(g_gattObjectMutex);
2486 jstring jni_remoteAddress = CALEClientGetAddressFromGattObj(env, gatt);
2487 if (!jni_remoteAddress)
2489 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
2490 ca_mutex_unlock(g_gattObjectMutex);
2491 return CA_STATUS_FAILED;
2494 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2497 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2498 ca_mutex_unlock(g_gattObjectMutex);
2499 return CA_STATUS_FAILED;
2502 if (!CALEClientIsGattObjInList(env, remoteAddress))
2504 jobject newGatt = (*env)->NewGlobalRef(env, gatt);
2505 u_arraylist_add(g_gattObjectList, newGatt);
2506 OIC_LOG(DEBUG, TAG, "Set GATT Object to Array as Element");
2509 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2510 ca_mutex_unlock(g_gattObjectMutex);
2511 return CA_STATUS_OK;
2514 bool CALEClientIsGattObjInList(JNIEnv *env, const char* remoteAddress)
2516 OIC_LOG(DEBUG, TAG, "CALEClientIsGattObjInList");
2517 VERIFY_NON_NULL(env, TAG, "env is null");
2518 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", true);
2520 uint32_t length = u_arraylist_length(g_gattObjectList);
2521 for (uint32_t index = 0; index < length; index++)
2524 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2527 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2531 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2532 if (!jni_setAddress)
2534 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2538 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2541 OIC_LOG(ERROR, TAG, "setAddress is null");
2545 if (!strcmp(remoteAddress, setAddress))
2547 OIC_LOG(DEBUG, TAG, "the device is already set");
2548 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2553 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2558 OIC_LOG(DEBUG, TAG, "There are no GATT object in list. it can be added");
2562 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress)
2564 OIC_LOG(DEBUG, TAG, "CALEClientGetGattObjInList");
2565 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
2566 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", NULL);
2568 ca_mutex_lock(g_gattObjectMutex);
2569 uint32_t length = u_arraylist_length(g_gattObjectList);
2570 for (uint32_t index = 0; index < length; index++)
2572 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2575 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2576 ca_mutex_unlock(g_gattObjectMutex);
2580 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2581 if (!jni_setAddress)
2583 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2584 ca_mutex_unlock(g_gattObjectMutex);
2588 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2591 OIC_LOG(ERROR, TAG, "setAddress is null");
2592 ca_mutex_unlock(g_gattObjectMutex);
2596 if (!strcmp(remoteAddress, setAddress))
2598 OIC_LOG(DEBUG, TAG, "the device is already set");
2599 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2600 ca_mutex_unlock(g_gattObjectMutex);
2603 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2606 ca_mutex_unlock(g_gattObjectMutex);
2607 OIC_LOG(DEBUG, TAG, "There are no the gatt object in list");
2611 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env)
2613 OIC_LOG(DEBUG, TAG, "CALEClientRemoveAllGattObjs");
2614 VERIFY_NON_NULL(env, TAG, "env is null");
2616 ca_mutex_lock(g_gattObjectMutex);
2617 if (!g_gattObjectList)
2619 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
2620 ca_mutex_unlock(g_gattObjectMutex);
2621 return CA_STATUS_FAILED;
2624 uint32_t length = u_arraylist_length(g_gattObjectList);
2625 for (uint32_t index = 0; index < length; index++)
2627 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2630 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2633 (*env)->DeleteGlobalRef(env, jarrayObj);
2636 OICFree(g_gattObjectList);
2637 g_gattObjectList = NULL;
2638 ca_mutex_unlock(g_gattObjectMutex);
2639 return CA_STATUS_OK;
2642 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt)
2644 OIC_LOG(DEBUG, TAG, "CALEClientRemoveGattObj");
2645 VERIFY_NON_NULL(gatt, TAG, "gatt is null");
2646 VERIFY_NON_NULL(env, TAG, "env is null");
2648 ca_mutex_lock(g_gattObjectMutex);
2649 if (!g_gattObjectList)
2651 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
2652 ca_mutex_unlock(g_gattObjectMutex);
2653 return CA_STATUS_FAILED;
2656 uint32_t length = u_arraylist_length(g_gattObjectList);
2657 for (uint32_t index = 0; index < length; index++)
2659 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2662 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2663 ca_mutex_unlock(g_gattObjectMutex);
2664 return CA_STATUS_FAILED;
2667 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2668 if (!jni_setAddress)
2670 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2671 ca_mutex_unlock(g_gattObjectMutex);
2672 return CA_STATUS_FAILED;
2675 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2678 OIC_LOG(ERROR, TAG, "setAddress is null");
2679 ca_mutex_unlock(g_gattObjectMutex);
2680 return CA_STATUS_FAILED;
2683 jstring jni_remoteAddress = CALEClientGetAddressFromGattObj(env, gatt);
2684 if (!jni_remoteAddress)
2686 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
2687 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2688 ca_mutex_unlock(g_gattObjectMutex);
2689 return CA_STATUS_FAILED;
2692 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2695 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2696 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2697 ca_mutex_unlock(g_gattObjectMutex);
2698 return CA_STATUS_FAILED;
2701 if (!strcmp(setAddress, remoteAddress))
2703 OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
2704 (*env)->DeleteGlobalRef(env, jarrayObj);
2705 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2706 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2708 if (NULL == u_arraylist_remove(g_gattObjectList, index))
2710 OIC_LOG(ERROR, TAG, "List removal failed.");
2711 ca_mutex_unlock(g_gattObjectMutex);
2712 return CA_STATUS_FAILED;
2714 ca_mutex_unlock(g_gattObjectMutex);
2715 return CA_STATUS_OK;
2717 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2718 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2721 ca_mutex_unlock(g_gattObjectMutex);
2722 OIC_LOG(DEBUG, TAG, "there are no target object");
2723 return CA_STATUS_OK;
2726 CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr)
2728 OIC_LOG(DEBUG, TAG, "CALEClientRemoveGattObjForAddr");
2729 VERIFY_NON_NULL(addr, TAG, "addr is null");
2730 VERIFY_NON_NULL(env, TAG, "env is null");
2732 ca_mutex_lock(g_gattObjectMutex);
2733 if (!g_gattObjectList)
2735 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
2736 ca_mutex_unlock(g_gattObjectMutex);
2737 return CA_STATUS_FAILED;
2740 uint32_t length = u_arraylist_length(g_gattObjectList);
2741 for (uint32_t index = 0; index < length; index++)
2743 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2746 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2747 ca_mutex_unlock(g_gattObjectMutex);
2748 return CA_STATUS_FAILED;
2751 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2752 if (!jni_setAddress)
2754 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2755 ca_mutex_unlock(g_gattObjectMutex);
2756 return CA_STATUS_FAILED;
2759 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2762 OIC_LOG(ERROR, TAG, "setAddress is null");
2763 ca_mutex_unlock(g_gattObjectMutex);
2764 return CA_STATUS_FAILED;
2767 const char* remoteAddress = (*env)->GetStringUTFChars(env, addr, NULL);
2770 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2771 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2772 ca_mutex_unlock(g_gattObjectMutex);
2773 return CA_STATUS_FAILED;
2776 if (!strcmp(setAddress, remoteAddress))
2778 OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
2779 (*env)->DeleteGlobalRef(env, jarrayObj);
2781 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2782 (*env)->ReleaseStringUTFChars(env, addr, remoteAddress);
2783 if (NULL == u_arraylist_remove(g_gattObjectList, index))
2785 OIC_LOG(ERROR, TAG, "List removal failed.");
2786 ca_mutex_unlock(g_gattObjectMutex);
2787 return CA_STATUS_FAILED;
2789 ca_mutex_unlock(g_gattObjectMutex);
2790 return CA_STATUS_OK;
2792 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2793 (*env)->ReleaseStringUTFChars(env, addr, remoteAddress);
2796 ca_mutex_unlock(g_gattObjectMutex);
2797 OIC_LOG(DEBUG, TAG, "there are no target object");
2798 return CA_STATUS_FAILED;
2805 CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
2806 uint16_t notificationState, uint16_t sendState)
2808 VERIFY_NON_NULL(address, TAG, "address is null");
2810 CALEState_t *newstate = (CALEState_t*) OICMalloc(sizeof(CALEState_t));
2813 OIC_LOG(ERROR, TAG, "out of memory");
2814 return CA_MEMORY_ALLOC_FAILED;
2817 if (strlen(address) > CA_MACADDR_SIZE)
2819 OIC_LOG(ERROR, TAG, "address is not proper");
2821 return CA_STATUS_FAILED;
2824 OICStrcpy(newstate->address, sizeof(newstate->address), address);
2825 newstate->connectedState = connectedState;
2826 newstate->notificationState = notificationState;
2827 newstate->sendState = sendState;
2828 return CALEClientAddDeviceStateToList(newstate);
2831 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state)
2833 VERIFY_NON_NULL(state, TAG, "state is null");
2835 ca_mutex_lock(g_deviceStateListMutex);
2837 if (!g_deviceStateList)
2839 OIC_LOG(ERROR, TAG, "gdevice_list is null");
2840 ca_mutex_unlock(g_deviceStateListMutex);
2841 return CA_STATUS_FAILED;
2844 if (CALEClientIsDeviceInList(state->address))
2846 CALEState_t* curState = CALEClientGetStateInfo(state->address);
2849 OIC_LOG(ERROR, TAG, "curState is null");
2850 ca_mutex_unlock(g_deviceStateListMutex);
2851 return CA_STATUS_FAILED;
2854 if (STATE_CHARACTER_NO_CHANGE == state->notificationState)
2856 state->notificationState = curState->notificationState;
2859 // delete previous state for update new state
2860 CAResult_t res = CALEClientRemoveDeviceState(state->address);
2861 if (CA_STATUS_OK != res)
2863 OIC_LOG(ERROR, TAG, "CALEClientRemoveDeviceState has failed");
2864 ca_mutex_unlock(g_deviceStateListMutex);
2868 u_arraylist_add(g_deviceStateList, state); // update new state
2869 OIC_LOG_V(DEBUG, TAG, "Set State Info to List : %d, %d",
2870 state->connectedState, state->notificationState);
2872 ca_mutex_unlock(g_deviceStateListMutex);
2873 return CA_STATUS_OK;
2876 bool CALEClientIsDeviceInList(const char* remoteAddress)
2878 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
2880 if (!g_deviceStateList)
2882 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2886 uint32_t length = u_arraylist_length(g_deviceStateList);
2887 for (uint32_t index = 0; index < length; index++)
2889 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2892 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2896 if (!strcmp(remoteAddress, state->address))
2898 OIC_LOG(DEBUG, TAG, "the device is already set");
2907 OIC_LOG(DEBUG, TAG, "there are no the device in list.");
2911 CAResult_t CALEClientRemoveAllDeviceState()
2913 OIC_LOG(DEBUG, TAG, "CALENativeRemoveAllDevices");
2915 ca_mutex_lock(g_deviceStateListMutex);
2916 if (!g_deviceStateList)
2918 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2919 ca_mutex_unlock(g_deviceStateListMutex);
2920 return CA_STATUS_FAILED;
2923 uint32_t length = u_arraylist_length(g_deviceStateList);
2924 for (uint32_t index = 0; index < length; index++)
2926 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2929 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2935 OICFree(g_deviceStateList);
2936 g_deviceStateList = NULL;
2937 ca_mutex_unlock(g_deviceStateListMutex);
2939 return CA_STATUS_OK;
2942 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress)
2944 OIC_LOG(DEBUG, TAG, "CALEClientRemoveDeviceState");
2945 VERIFY_NON_NULL(remoteAddress, TAG, "remoteAddress is null");
2947 if (!g_deviceStateList)
2949 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2950 return CA_STATUS_FAILED;
2953 uint32_t length = u_arraylist_length(g_deviceStateList);
2954 for (uint32_t index = 0; index < length; index++)
2956 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2959 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2963 if (!strcmp(state->address, remoteAddress))
2965 OIC_LOG_V(DEBUG, TAG, "remove state : %s", remoteAddress);
2968 if (NULL == u_arraylist_remove(g_deviceStateList, index))
2970 OIC_LOG(ERROR, TAG, "List removal failed.");
2971 return CA_STATUS_FAILED;
2974 return CA_STATUS_OK;
2978 return CA_STATUS_FAILED;
2981 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress)
2983 OIC_LOG(DEBUG, TAG, "CALEClientGetStateInfo");
2984 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", NULL);
2986 if (!g_deviceStateList)
2988 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2992 uint32_t length = u_arraylist_length(g_deviceStateList);
2993 for (uint32_t index = 0; index < length; index++)
2995 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2998 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
3002 if (!strcmp(state->address, remoteAddress))
3004 OIC_LOG_V(DEBUG, TAG, "get state : %s", remoteAddress);
3011 bool CALEClientIsConnectedDevice(const char* remoteAddress)
3013 OIC_LOG(DEBUG, TAG, "CALEClientIsConnectedDevice");
3014 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
3016 ca_mutex_lock(g_deviceStateListMutex);
3017 if (!g_deviceStateList)
3019 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
3020 ca_mutex_unlock(g_deviceStateListMutex);
3024 uint32_t length = u_arraylist_length(g_deviceStateList);
3025 for (uint32_t index = 0; index < length; index++)
3027 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
3030 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
3034 if (!strcmp(state->address, remoteAddress))
3036 OIC_LOG(DEBUG, TAG, "check whether it is connected or not");
3038 if (STATE_CONNECTED == state->connectedState)
3040 ca_mutex_unlock(g_deviceStateListMutex);
3045 ca_mutex_unlock(g_deviceStateListMutex);
3050 ca_mutex_unlock(g_deviceStateListMutex);
3054 bool CALEClientIsSetCharacteristic(const char* remoteAddress)
3056 OIC_LOG(DEBUG, TAG, "CALEClientIsSetCharacteristic");
3057 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
3059 ca_mutex_lock(g_deviceStateListMutex);
3060 if (!g_deviceStateList)
3062 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
3063 ca_mutex_unlock(g_deviceStateListMutex);
3067 uint32_t length = u_arraylist_length(g_deviceStateList);
3068 for (uint32_t index = 0; index < length; index++)
3070 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
3073 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
3077 if (!strcmp(state->address, remoteAddress))
3079 OIC_LOG_V(DEBUG, TAG, "check whether it was set or not:%d", state->notificationState);
3081 if (STATE_CHARACTER_SET == state->notificationState)
3083 ca_mutex_unlock(g_deviceStateListMutex);
3088 ca_mutex_unlock(g_deviceStateListMutex);
3094 ca_mutex_unlock(g_deviceStateListMutex);
3098 void CALEClientCreateDeviceList()
3100 OIC_LOG(DEBUG, TAG, "CALEClientCreateDeviceList");
3102 // create new object array
3103 if (!g_gattObjectList)
3105 OIC_LOG(DEBUG, TAG, "Create g_gattObjectList");
3107 g_gattObjectList = u_arraylist_create();
3110 if (!g_deviceStateList)
3112 OIC_LOG(DEBUG, TAG, "Create g_deviceStateList");
3114 g_deviceStateList = u_arraylist_create();
3119 OIC_LOG(DEBUG, TAG, "Create g_deviceList");
3121 g_deviceList = u_arraylist_create();
3126 * Check Sent Count for remove g_sendBuffer
3128 void CALEClientUpdateSendCnt(JNIEnv *env)
3130 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3132 ca_mutex_lock(g_threadMutex);
3136 if (g_targetCnt <= g_currentSentCnt)
3139 g_currentSentCnt = 0;
3143 (*env)->DeleteGlobalRef(env, g_sendBuffer);
3144 g_sendBuffer = NULL;
3146 // notity the thread
3147 ca_cond_signal(g_threadCond);
3149 CALEClientSetSendFinishFlag(true);
3150 OIC_LOG(DEBUG, TAG, "set signal for send data");
3153 ca_mutex_unlock(g_threadMutex);
3156 CAResult_t CALEClientInitGattMutexVaraibles()
3158 if (NULL == g_bleReqRespClientCbMutex)
3160 g_bleReqRespClientCbMutex = ca_mutex_new();
3161 if (NULL == g_bleReqRespClientCbMutex)
3163 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3164 return CA_STATUS_FAILED;
3168 if (NULL == g_bleServerBDAddressMutex)
3170 g_bleServerBDAddressMutex = ca_mutex_new();
3171 if (NULL == g_bleServerBDAddressMutex)
3173 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3174 return CA_STATUS_FAILED;
3178 if (NULL == g_threadMutex)
3180 g_threadMutex = ca_mutex_new();
3181 if (NULL == g_threadMutex)
3183 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3184 return CA_STATUS_FAILED;
3188 if (NULL == g_threadSendMutex)
3190 g_threadSendMutex = ca_mutex_new();
3191 if (NULL == g_threadSendMutex)
3193 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3194 return CA_STATUS_FAILED;
3198 if (NULL == g_deviceListMutex)
3200 g_deviceListMutex = ca_mutex_new();
3201 if (NULL == g_deviceListMutex)
3203 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3204 return CA_STATUS_FAILED;
3208 if (NULL == g_gattObjectMutex)
3210 g_gattObjectMutex = ca_mutex_new();
3211 if (NULL == g_gattObjectMutex)
3213 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3214 return CA_STATUS_FAILED;
3218 if (NULL == g_deviceStateListMutex)
3220 g_deviceStateListMutex = ca_mutex_new();
3221 if (NULL == g_deviceStateListMutex)
3223 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3224 return CA_STATUS_FAILED;
3228 if (NULL == g_SendFinishMutex)
3230 g_SendFinishMutex = ca_mutex_new();
3231 if (NULL == g_SendFinishMutex)
3233 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3234 return CA_STATUS_FAILED;
3238 if (NULL == g_scanMutex)
3240 g_scanMutex = ca_mutex_new();
3241 if (NULL == g_scanMutex)
3243 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3244 return CA_STATUS_FAILED;
3248 if (NULL == g_threadWriteCharacteristicMutex)
3250 g_threadWriteCharacteristicMutex = ca_mutex_new();
3251 if (NULL == g_threadWriteCharacteristicMutex)
3253 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3254 return CA_STATUS_FAILED;
3258 return CA_STATUS_OK;
3261 void CALEClientTerminateGattMutexVariables()
3263 ca_mutex_free(g_bleReqRespClientCbMutex);
3264 g_bleReqRespClientCbMutex = NULL;
3266 ca_mutex_free(g_bleServerBDAddressMutex);
3267 g_bleServerBDAddressMutex = NULL;
3269 ca_mutex_free(g_threadMutex);
3270 g_threadMutex = NULL;
3272 ca_mutex_free(g_threadSendMutex);
3273 g_threadSendMutex = NULL;
3275 ca_mutex_free(g_deviceListMutex);
3276 g_deviceListMutex = NULL;
3278 ca_mutex_free(g_SendFinishMutex);
3279 g_SendFinishMutex = NULL;
3281 ca_mutex_free(g_scanMutex);
3284 ca_mutex_free(g_threadWriteCharacteristicMutex);
3285 g_threadWriteCharacteristicMutex = NULL;
3288 void CALEClientSetSendFinishFlag(bool flag)
3290 OIC_LOG_V(DEBUG, TAG, "g_isFinishedSendData is %d", flag);
3292 ca_mutex_lock(g_SendFinishMutex);
3293 g_isFinishedSendData = flag;
3294 ca_mutex_unlock(g_SendFinishMutex);
3301 CAResult_t CAStartLEGattClient()
3303 CAResult_t res = CALEClientStartMulticastServer();
3304 if (CA_STATUS_OK != res)
3306 OIC_LOG(ERROR, TAG, "CALEClientStartMulticastServer has failed");
3310 g_isStartedLEClient = true;
3316 void CAStopLEGattClient()
3318 OIC_LOG(DEBUG, TAG, "CAStopBLEGattClient");
3322 OIC_LOG(ERROR, TAG, "g_jvm is null");
3326 bool isAttached = false;
3328 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
3331 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
3332 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
3336 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
3342 CAResult_t ret = CALEClientDisconnectAll(env);
3343 if (CA_STATUS_OK != ret)
3345 OIC_LOG(ERROR, TAG, "CALEClientDisconnectAll has failed");
3348 ret = CALEClientStopScan();
3349 if(CA_STATUS_OK != ret)
3351 OIC_LOG(ERROR, TAG, "CALEClientStopScan has failed");
3354 ca_mutex_lock(g_threadMutex);
3355 ca_cond_signal(g_threadCond);
3356 ca_mutex_unlock(g_threadMutex);
3358 ca_mutex_lock(g_threadWriteCharacteristicMutex);
3359 ca_cond_signal(g_threadWriteCharacteristicCond);
3360 ca_mutex_unlock(g_threadWriteCharacteristicMutex);
3364 (*g_jvm)->DetachCurrentThread(g_jvm);
3369 CAResult_t CAInitializeLEGattClient()
3371 OIC_LOG(DEBUG, TAG, "Initialize GATT Client");
3372 CALEClientInitialize();
3373 return CA_STATUS_OK;
3376 void CATerminateLEGattClient()
3378 OIC_LOG(DEBUG, TAG, "Terminate GATT Client");
3379 CALEClientTerminate();
3382 CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress, const uint8_t *data,
3383 uint32_t dataLen, CALETransferType_t type,
3386 OIC_LOG(DEBUG, TAG, "call CALEClientSendUnicastMessage");
3387 VERIFY_NON_NULL(data, TAG, "data is null");
3388 VERIFY_NON_NULL(remoteAddress, TAG, "remoteAddress is null");
3390 if (LE_UNICAST != type || position < 0)
3392 OIC_LOG(ERROR, TAG, "this request is not unicast");
3393 return CA_STATUS_INVALID_PARAM;
3396 return CALEClientSendUnicastMessage(remoteAddress, data, dataLen);
3399 CAResult_t CAUpdateCharacteristicsToAllGattServers(const uint8_t *data, uint32_t dataLen)
3401 OIC_LOG(DEBUG, TAG, "call CALEClientSendMulticastMessage");
3402 VERIFY_NON_NULL(data, TAG, "data is null");
3404 return CALEClientSendMulticastMessage(data, dataLen);
3407 void CASetLEReqRespClientCallback(CABLEDataReceivedCallback callback)
3409 ca_mutex_lock(g_bleReqRespClientCbMutex);
3410 g_CABLEClientDataReceivedCallback = callback;
3411 ca_mutex_unlock(g_bleReqRespClientCbMutex);
3414 void CASetLEClientThreadPoolHandle(ca_thread_pool_t handle)
3416 g_threadPoolHandle = handle;
3419 CAResult_t CAGetLEAddress(char **local_address)
3421 VERIFY_NON_NULL(local_address, TAG, "local_address");
3422 OIC_LOG(INFO, TAG, "CAGetLEAddress is not support");
3423 return CA_NOT_SUPPORTED;
3426 JNIEXPORT void JNICALL
3427 Java_org_iotivity_ca_CaLeClientInterface_caLeRegisterLeScanCallback(JNIEnv *env, jobject obj,
3430 OIC_LOG(DEBUG, TAG, "CaLeRegisterLeScanCallback");
3431 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3432 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3433 VERIFY_NON_NULL_VOID(callback, TAG, "callback is null");
3435 g_leScanCallback = (*env)->NewGlobalRef(env, callback);
3438 JNIEXPORT void JNICALL
3439 Java_org_iotivity_ca_CaLeClientInterface_caLeRegisterGattCallback(JNIEnv *env, jobject obj,
3442 OIC_LOG(DEBUG, TAG, "CaLeRegisterGattCallback");
3443 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3444 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3445 VERIFY_NON_NULL_VOID(callback, TAG, "callback is null");
3447 g_leGattCallback = (*env)->NewGlobalRef(env, callback);
3450 JNIEXPORT void JNICALL
3451 Java_org_iotivity_ca_CaLeClientInterface_caLeScanCallback(JNIEnv *env, jobject obj,
3454 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3455 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3456 VERIFY_NON_NULL_VOID(device, TAG, "device is null");
3458 CAResult_t res = CALEClientAddScanDeviceToList(env, device);
3459 if (CA_STATUS_OK != res)
3461 OIC_LOG_V(ERROR, TAG, "CALEClientAddScanDeviceToList has failed : %d", res);
3466 * Class: org_iotivity_ca_jar_caleinterface
3467 * Method: CALeGattConnectionStateChangeCallback
3468 * Signature: (Landroid/bluetooth/BluetoothGatt;II)V
3470 JNIEXPORT void JNICALL
3471 Java_org_iotivity_ca_CaLeClientInterface_caLeGattConnectionStateChangeCallback(JNIEnv *env,
3477 OIC_LOG_V(DEBUG, TAG, "CALeGattConnectionStateChangeCallback - status %d, newstate %d", status,
3479 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3480 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3481 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3483 if (GATT_SUCCESS == status && STATE_CONNECTED == newstate) // le connected
3485 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3491 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3494 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED,
3495 STATE_CHARACTER_NO_CHANGE,
3497 if (CA_STATUS_OK != res)
3499 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3500 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3503 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3506 CAResult_t res = CALEClientAddGattobjToList(env, gatt);
3507 if (CA_STATUS_OK != res)
3509 OIC_LOG(ERROR, TAG, "CALEClientAddGattobjToList has failed");
3513 res = CALEClientDiscoverServices(env, gatt);
3514 if (CA_STATUS_OK != res)
3516 OIC_LOG(ERROR, TAG, "CALEClientDiscoverServices has failed");
3520 else // le disconnected
3522 CAResult_t res = CALEClientStartScan();
3523 if (CA_STATUS_OK != res)
3525 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
3529 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3532 OIC_LOG(ERROR, TAG, "CALEClientGetAddressFromGattObj has failed");
3536 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3539 res = CALEClientRemoveDeviceState(address);
3540 if (CA_STATUS_OK != res)
3542 OIC_LOG(ERROR, TAG, "CALEClientRemoveDeviceState has failed");
3546 res = CALEClientRemoveGattObjForAddr(env, jni_address);
3547 if (CA_STATUS_OK != res)
3549 OIC_LOG(ERROR, TAG, "CALEClientRemoveGattObjForAddr has failed");
3553 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3556 res = CALEClientGattClose(env, gatt);
3557 if (CA_STATUS_OK != res)
3559 OIC_LOG(ERROR, TAG, "CALEClientGattClose has failed");
3564 (*env)->DeleteGlobalRef(env, g_sendBuffer);
3565 g_sendBuffer = NULL;
3573 CALEClientSendFinish(env, gatt);
3578 * Class: org_iotivity_ca_jar_caleinterface
3579 * Method: CALeGattServicesDiscoveredCallback
3580 * Signature: (Landroid/bluetooth/BluetoothGatt;I)V
3582 JNIEXPORT void JNICALL
3583 Java_org_iotivity_ca_CaLeClientInterface_caLeGattServicesDiscoveredCallback(JNIEnv *env,
3588 OIC_LOG_V(DEBUG, TAG, "CALeGattServicesDiscoveredCallback - status %d: ", status);
3589 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3590 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3591 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3593 if (0 != status) // discovery error
3595 CALEClientSendFinish(env, gatt);
3599 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3602 CALEClientSendFinish(env, gatt);
3606 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3609 CALEClientSendFinish(env, gatt);
3613 if (!CALEClientIsSetCharacteristic(address))
3615 jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_RESPONSE_UUID);
3618 OIC_LOG(ERROR, TAG, "jni_uuid is null");
3622 jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, gatt, jni_uuid);
3623 if (!jni_obj_GattCharacteristic)
3625 OIC_LOG(ERROR, TAG, "jni_obj_GattCharacteristic is null");
3629 CAResult_t res = CALEClientSetCharacteristicNotification(env, gatt,
3630 jni_obj_GattCharacteristic);
3631 if (CA_STATUS_OK != res)
3633 OIC_LOG(ERROR, TAG, "CALEClientSetCharacteristicNotification has failed");
3637 res = CALEClientSetUUIDToDescriptor(env, gatt, jni_obj_GattCharacteristic);
3638 if (CA_STATUS_OK != res)
3640 OIC_LOG_V(INFO, TAG, "Descriptor is not found : %d", res);
3643 CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
3644 if (CA_STATUS_OK != res)
3646 OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
3652 res = CALEClientUpdateDeviceState(address, STATE_CONNECTED, STATE_CHARACTER_SET,
3654 if (CA_STATUS_OK != res)
3656 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3662 CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
3663 if (CA_STATUS_OK != res)
3665 OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
3669 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3674 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3675 CALEClientSendFinish(env, gatt);
3680 * Class: org_iotivity_ca_jar_caleinterface
3681 * Method: CALeGattCharacteristicWritjclasseCallback
3682 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;I)V
3684 JNIEXPORT void JNICALL
3685 Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicWriteCallback(
3686 JNIEnv *env, jobject obj, jobject gatt, jbyteArray data,
3689 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - status : %d", status);
3690 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3691 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3692 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3695 char* wroteData = (char*) (*env)->GetByteArrayElements(env, data, &isCopy);
3697 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - write data : %s", wroteData);
3699 // send success & signal
3700 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3706 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3712 if (GATT_SUCCESS != status) // error case
3714 OIC_LOG(ERROR, TAG, "send failure");
3717 CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
3718 if (CA_STATUS_OK != res)
3720 OIC_LOG(ERROR, TAG, "WriteCharacteristic has failed");
3721 ca_mutex_lock(g_threadWriteCharacteristicMutex);
3722 g_isSignalSetFlag = true;
3723 ca_cond_signal(g_threadWriteCharacteristicCond);
3724 ca_mutex_unlock(g_threadWriteCharacteristicMutex);
3726 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED,
3727 STATE_CHARACTER_SET,
3729 if (CA_STATUS_OK != res)
3731 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3734 if (g_clientErrorCallback)
3736 jint length = (*env)->GetArrayLength(env, data);
3737 g_clientErrorCallback(address, data, length, CA_SEND_FAILED);
3740 CALEClientSendFinish(env, gatt);
3746 OIC_LOG(DEBUG, TAG, "send success");
3747 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED, STATE_CHARACTER_SET,
3748 STATE_SEND_SUCCESS);
3749 if (CA_STATUS_OK != res)
3751 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3754 ca_mutex_lock(g_threadWriteCharacteristicMutex);
3755 OIC_LOG(DEBUG, TAG, "g_isSignalSetFlag is set true and signal");
3756 g_isSignalSetFlag = true;
3757 ca_cond_signal(g_threadWriteCharacteristicCond);
3758 ca_mutex_unlock(g_threadWriteCharacteristicMutex);
3760 CALEClientUpdateSendCnt(env);
3763 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3769 CALEClientSendFinish(env, gatt);
3774 * Class: org_iotivity_ca_jar_caleinterface
3775 * Method: CALeGattCharacteristicChangedCallback
3776 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;)V
3778 JNIEXPORT void JNICALL
3779 Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicChangedCallback(
3780 JNIEnv *env, jobject obj, jobject gatt, jbyteArray data)
3782 OIC_LOG(DEBUG, TAG, "CALeGattCharacteristicChangedCallback");
3783 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3784 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3785 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3786 VERIFY_NON_NULL_VOID(data, TAG, "data is null");
3788 // get Byte Array and convert to uint8_t*
3789 jint length = (*env)->GetArrayLength(env, data);
3792 jbyte *jni_byte_responseData = (jbyte*) (*env)->GetByteArrayElements(env, data, &isCopy);
3794 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback - raw data received : %p",
3795 jni_byte_responseData);
3797 uint8_t* receivedData = OICMalloc(length);
3800 OIC_LOG(ERROR, TAG, "receivedData is null");
3804 memcpy(receivedData, jni_byte_responseData, length);
3805 (*env)->ReleaseByteArrayElements(env, data, jni_byte_responseData, JNI_ABORT);
3807 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3810 OIC_LOG(ERROR, TAG, "jni_address is null");
3811 OICFree(receivedData);
3815 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3818 OIC_LOG(ERROR, TAG, "address is null");
3819 OICFree(receivedData);
3823 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback - data. : %p, %d",
3824 receivedData, length);
3826 ca_mutex_lock(g_bleServerBDAddressMutex);
3827 uint32_t sentLength = 0;
3828 g_CABLEClientDataReceivedCallback(address, receivedData, length,
3830 ca_mutex_unlock(g_bleServerBDAddressMutex);
3832 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3836 * Class: org_iotivity_ca_jar_caleinterface
3837 * Method: CALeGattDescriptorWriteCallback
3838 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattDescriptor;I)V
3840 JNIEXPORT void JNICALL
3841 Java_org_iotivity_ca_CaLeClientInterface_caLeGattDescriptorWriteCallback(JNIEnv *env, jobject obj,
3845 OIC_LOG_V(DEBUG, TAG, "CALeGattDescriptorWriteCallback - status %d: ", status);
3846 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3847 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
3848 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3850 CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
3851 if (CA_STATUS_OK != res)
3853 OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
3861 CALEClientSendFinish(env, gatt);