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 ******************************************************************/
23 #include <android/log.h>
24 #include "caedrutils.h"
26 #include "oic_malloc.h"
27 #include "oic_string.h"
28 #include "cathreadpool.h"
29 #include "uarraylist.h"
31 #define ERROR_CODE (-1)
32 #define TAG PCF("CA_EDR_UTILS")
34 static const char METHODID_OBJECTNONPARAM[] = "()Landroid/bluetooth/BluetoothAdapter;";
35 static const char METHODID_STRINGNONPARAM[] = "()Ljava/lang/String;";
36 static const char CLASSPATH_BT_ADPATER[] = "android/bluetooth/BluetoothAdapter";
37 static const char CLASSPATH_BT_DEVICE[] = "android/bluetooth/BluetoothDevice";
38 static const char CLASSPATH_BT_SOCKET[] = "android/bluetooth/BluetoothSocket";
40 static u_arraylist_t *g_deviceStateList = NULL;
41 static u_arraylist_t *g_deviceObjectList = NULL;
43 // get address from bluetooth socket
44 jstring CAEDRNativeGetAddressFromDeviceSocket(JNIEnv *env, jobject bluetoothSocketObj)
46 if (!bluetoothSocketObj)
48 OIC_LOG(ERROR, TAG, "[EDR] getRemoteAddress: bluetoothSocketObj is null");
52 jclass jni_cid_BTSocket = (*env)->FindClass(env, CLASSPATH_BT_SOCKET);
53 if (!jni_cid_BTSocket)
55 OIC_LOG(ERROR, TAG, "[EDR] getRemoteAddress: jni_cid_BTSocket is null");
59 jmethodID jni_mid_getRemoteDevice = (*env)->GetMethodID(
60 env, jni_cid_BTSocket, "getRemoteDevice", "()Landroid/bluetooth/BluetoothDevice;");
61 if (!jni_mid_getRemoteDevice)
63 (*env)->DeleteLocalRef(env, jni_cid_BTSocket);
65 OIC_LOG(ERROR, TAG, "[EDR] getRemoteAddress: jni_mid_getRemoteDevice is null");
69 jobject jni_obj_remoteBTDevice = (*env)->CallObjectMethod(env, bluetoothSocketObj,
70 jni_mid_getRemoteDevice);
71 if (!jni_obj_remoteBTDevice)
73 (*env)->DeleteLocalRef(env, jni_cid_BTSocket);
75 OIC_LOG(ERROR, TAG, "[EDR] getRemoteAddress: jni_obj_remoteBTDevice is null");
79 jclass jni_cid_BTDevice = (*env)->FindClass(env, CLASSPATH_BT_DEVICE);
80 if (!jni_cid_BTDevice)
82 (*env)->DeleteLocalRef(env, jni_obj_remoteBTDevice);
83 (*env)->DeleteLocalRef(env, jni_cid_BTSocket);
85 OIC_LOG(ERROR, TAG, "[EDR] getRemoteAddress: jni_cid_BTDevice is null");
88 jmethodID j_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTDevice, "getAddress",
89 METHODID_STRINGNONPARAM);
90 if (!j_mid_getAddress)
92 (*env)->DeleteLocalRef(env, jni_obj_remoteBTDevice);
93 (*env)->DeleteLocalRef(env, jni_cid_BTDevice);
94 (*env)->DeleteLocalRef(env, jni_cid_BTSocket);
96 OIC_LOG(ERROR, TAG, "[EDR] getRemoteAddress: j_mid_getAddress is null");
100 jstring j_str_address = (*env)->CallObjectMethod(env, jni_obj_remoteBTDevice, j_mid_getAddress);
103 (*env)->DeleteLocalRef(env, jni_obj_remoteBTDevice);
104 (*env)->DeleteLocalRef(env, jni_cid_BTDevice);
105 (*env)->DeleteLocalRef(env, jni_cid_BTSocket);
107 OIC_LOG(ERROR, TAG, "[EDR] getRemoteAddress: j_str_address is null");
111 (*env)->DeleteLocalRef(env, jni_obj_remoteBTDevice);
112 (*env)->DeleteLocalRef(env, jni_cid_BTDevice);
113 (*env)->DeleteLocalRef(env, jni_cid_BTSocket);
115 return j_str_address;
118 jstring CAEDRNativeGetLocalDeviceAddress(JNIEnv* env)
120 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
121 if (!jni_cid_BTAdapter)
123 OIC_LOG(ERROR, TAG, "[EDR][Native] getAddress: jni_cid_BTAdapter is null");
127 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
129 METHODID_OBJECTNONPARAM);
130 if (!jni_mid_getDefaultAdapter)
132 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
134 OIC_LOG(ERROR, TAG, "[EDR][Native] getAddress: jni_mid_getDefaultAdapter is null");
138 jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTAdapter, "getAddress",
139 METHODID_STRINGNONPARAM);
140 if (!jni_mid_getAddress)
142 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
144 OIC_LOG(ERROR, TAG, "[EDR][Native] getAddress: jni_mid_getAddress is null");
148 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
149 jni_mid_getDefaultAdapter);
150 if (!jni_obj_BTAdapter)
152 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
154 OIC_LOG(ERROR, TAG, "[EDR][Native] getAddress: jni_obj_BTAdapter is null");
158 jstring jni_str_address = (jstring)(*env)->CallObjectMethod(env, jni_obj_BTAdapter,
160 if (!jni_str_address)
162 (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
163 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
165 OIC_LOG(ERROR, TAG, "[EDR][Native] getAddress: jni_str_address is null");
169 (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
170 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
172 return jni_str_address;
175 jobjectArray CAEDRNativeGetBondedDevices(JNIEnv *env)
177 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
178 if (!jni_cid_BTAdapter)
180 OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: jni_cid_BTAdapter is null");
184 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
186 METHODID_OBJECTNONPARAM);
187 if (!jni_mid_getDefaultAdapter)
189 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
191 OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: default adapter is null");
195 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
196 jni_mid_getDefaultAdapter);
197 if (!jni_obj_BTAdapter)
199 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
201 OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: bluetooth adapter is null");
205 // Get a list of currently paired devices
206 jmethodID jni_mid_getBondedDevices = (*env)->GetMethodID(env, jni_cid_BTAdapter,
208 "()Ljava/util/Set;");
209 if (!jni_mid_getBondedDevices)
211 (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
212 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
214 OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: jni_mid_getBondedDevicesr is null");
218 jobject jni_obj_setPairedDevices = (*env)->CallObjectMethod(env, jni_obj_BTAdapter,
219 jni_mid_getBondedDevices);
220 if (!jni_obj_setPairedDevices)
222 (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
223 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
225 OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: jni_obj_setPairedDevices is null");
229 // Convert the set to an object array
230 // object[] array = Set<BluetoothDevice>.toArray();
231 jclass jni_cid_Set = (*env)->FindClass(env, "java/util/Set");
234 (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
235 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
237 OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: jni_cid_Set is null");
240 jmethodID jni_mid_toArray = (*env)->GetMethodID(env, jni_cid_Set, "toArray",
241 "()[Ljava/lang/Object;");
243 if (!jni_mid_toArray)
245 (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
246 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
248 OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: jni_mid_toArray is null");
252 jobjectArray jni_arrayPairedDevices = (jobjectArray)(
253 (*env)->CallObjectMethod(env, jni_obj_setPairedDevices, jni_mid_toArray));
254 if (!jni_arrayPairedDevices)
256 (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
257 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
259 OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: jni_arrayPairedDevices is null");
263 (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
264 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
266 return jni_arrayPairedDevices;
269 jint CAEDRNativeGetBTStateOnInfo(JNIEnv *env)
271 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
272 if (!jni_cid_BTAdapter)
274 OIC_LOG(ERROR, TAG, "[EDR][Native] getBTStateOnInfo: jni_cid_BTAdapter is null");
278 jfieldID jni_fid_stateon = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
279 if (jni_fid_stateon == 0)
281 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
283 OIC_LOG(ERROR, TAG, "[EDR][Native] get_field_state is 0");
286 jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, jni_fid_stateon);
288 OIC_LOG_V(DEBUG, TAG, "[EDR][Native] bluetooth state integer value : %d", jni_int_val);
290 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
295 jboolean CAEDRNativeIsEnableBTAdapter(JNIEnv *env)
297 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
298 if (!jni_cid_BTAdapter)
300 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_cid_BTAdapter: jni_cid_BTAdapter is null");
304 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
306 METHODID_OBJECTNONPARAM);
307 if (!jni_mid_getDefaultAdapter)
309 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
311 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_mid_getDefaultAdapter is null");
315 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
316 jni_mid_getDefaultAdapter);
317 if (!jni_obj_BTAdapter)
319 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
321 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_obj_BTAdapter is null");
326 jmethodID jni_mid_isEnable = (*env)->GetMethodID(env, jni_cid_BTAdapter, "isEnabled", "()Z");
327 if (!jni_mid_isEnable)
329 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
330 (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
332 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_mid_isEnable is null");
336 jboolean jni_isEnable = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_isEnable);
337 OIC_LOG_V(DEBUG, TAG, "[EDR][Native] adapter state is %d", jni_isEnable);
339 (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
340 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
345 jstring CAEDRNativeGetAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice)
347 if (!bluetoothDevice)
349 OIC_LOG(ERROR, TAG, "[EDR][Native] bluetoothDevice is null");
352 jclass jni_cid_device_list = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
353 if (!jni_cid_device_list)
355 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_cid_device_list is null");
359 jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_device_list, "getAddress",
360 METHODID_STRINGNONPARAM);
361 if (!jni_mid_getAddress)
363 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_mid_getAddress is null");
367 jstring jni_address = (jstring)(*env)->CallObjectMethod(env, bluetoothDevice,
371 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_address is null");
380 void CAEDRNativeCreateDeviceStateList()
382 OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRNativeCreateDeviceStateList");
384 // create new object array
385 if (NULL == g_deviceStateList)
387 OIC_LOG(DEBUG, TAG, "Create device list");
389 g_deviceStateList = u_arraylist_create();
393 void CAEDRUpdateDeviceState(CAConnectedState_t state, const char *address)
397 OIC_LOG(ERROR, TAG, "[EDR][Native] address is null");
400 state_t *newstate = (state_t*) OICCalloc(1, sizeof(state_t));
403 OIC_LOG(ERROR, TAG, "[EDR][Native] newstate is null");
406 OICStrcpy((char*) newstate->address, sizeof(newstate->address), address);
407 newstate->state = state;
409 CAEDRNativeAddDeviceStateToList(newstate);
412 void CAEDRNativeAddDeviceStateToList(state_t *state)
416 OIC_LOG(ERROR, TAG, "[EDR][Native] device is null");
420 if (!g_deviceStateList)
422 OIC_LOG(ERROR, TAG, "[EDR][Native] gdevice_list is null");
426 if (CAEDRNativeIsDeviceInList((const char*) state->address))
428 // delete previous state for update new state
429 CAEDRNativeRemoveDevice((const char*) state->address);
431 u_arraylist_add(g_deviceStateList, state); // update new state
432 OIC_LOG_V(DEBUG, TAG, "Set State Info to List : %d", state->state);
435 bool CAEDRNativeIsDeviceInList(const char* remoteAddress)
440 OIC_LOG(ERROR, TAG, "[EDR][Native] remoteAddress is null");
444 jint length = u_arraylist_length(g_deviceStateList);
445 for (index = 0; index < length; index++)
447 state_t* state = (state_t*) u_arraylist_get(g_deviceStateList, index);
450 OIC_LOG(ERROR, TAG, "[EDR][Native] state_t object is null");
454 if (!strcmp(remoteAddress, (const char*) state->address))
456 OIC_LOG(DEBUG, TAG, "the device is already set");
465 OIC_LOG(DEBUG, TAG, "there are no the device in list.");
469 void CAEDRNativeRemoveAllDeviceState()
471 OIC_LOG(DEBUG, TAG, "CAEDRNativeRemoveAllDevices");
473 if (!g_deviceStateList)
475 OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceStateList is null");
480 jint length = u_arraylist_length(g_deviceStateList);
481 for (index = 0; index < length; index++)
483 state_t* state = (state_t*) u_arraylist_get(g_deviceStateList, index);
486 OIC_LOG(DEBUG, TAG, "[EDR][Native] jarrayObj is null");
492 OICFree(g_deviceStateList);
493 g_deviceStateList = NULL;
497 void CAEDRNativeRemoveDevice(const char *remoteAddress)
499 OIC_LOG(DEBUG, TAG, "CAEDRNativeRemoveDeviceforStateList");
501 if (!g_deviceStateList)
503 OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceStateList is null");
508 OIC_LOG(ERROR, TAG, "[EDR][Native] remoteAddress is null");
513 jint length = u_arraylist_length(g_deviceStateList);
514 for (index = 0; index < length; index++)
516 state_t* state = (state_t*) u_arraylist_get(g_deviceStateList, index);
519 OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
523 if (!strcmp((const char*) state->address, remoteAddress))
525 OIC_LOG_V(DEBUG, TAG, "[EDR][Native] remove state : %s", remoteAddress);
528 CAEDRReorderingDeviceList(index);
535 CAConnectedState_t CAEDRIsConnectedDevice(const char *remoteAddress)
537 OIC_LOG(DEBUG, TAG, "CAEDRIsConnectedDevice");
541 OIC_LOG(ERROR, TAG, "[EDR][Native] remoteAddress is null");
542 return STATE_DISCONNECTED;
545 if (!g_deviceStateList)
547 OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceStateList is null");
548 return STATE_DISCONNECTED;
552 jint length = u_arraylist_length(g_deviceStateList);
553 for (index = 0; index < length; index++)
555 state_t* state = (state_t*) u_arraylist_get(g_deviceStateList, index);
558 OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
562 if (!strcmp((const char*) state->address, remoteAddress))
564 OIC_LOG(DEBUG, TAG, "[EDR][Native] check whether it is connected or not");
569 return STATE_DISCONNECTED;
572 void CAEDRReorderingDeviceList(uint32_t index)
574 if (index >= g_deviceStateList->length)
579 if (index < g_deviceStateList->length - 1)
581 memmove(&g_deviceStateList->data[index], &g_deviceStateList->data[index + 1],
582 (g_deviceStateList->length - index - 1) * sizeof(void *));
585 g_deviceStateList->size--;
586 g_deviceStateList->length--;
590 * Device Socket Object List
592 void CAEDRNativeCreateDeviceSocketList()
594 OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRNativeCreateDeviceSocketList");
596 // create new object array
597 if (NULL == g_deviceObjectList)
599 OIC_LOG(DEBUG, TAG, "Create Device object list");
601 g_deviceObjectList = u_arraylist_create();
605 void CAEDRNativeAddDeviceSocketToList(JNIEnv *env, jobject deviceSocket)
607 OIC_LOG(DEBUG, TAG, "[EDR][Native] CANativeAddDeviceobjToList");
611 OIC_LOG(ERROR, TAG, "[EDR][Native] Device is null");
615 if (!g_deviceObjectList)
617 OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
621 jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
622 if (!jni_remoteAddress)
624 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_remoteAddress is null");
628 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
630 if (!CAEDRNativeIsDeviceSocketInList(env, remoteAddress))
632 jobject gDeviceSocker = (*env)->NewGlobalRef(env, deviceSocket);
633 u_arraylist_add(g_deviceObjectList, gDeviceSocker);
634 OIC_LOG(DEBUG, TAG, "Set Socket Object to Array");
636 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
639 bool CAEDRNativeIsDeviceSocketInList(JNIEnv *env, const char* remoteAddress)
641 OIC_LOG(DEBUG, TAG, "[EDR][Native] CANativeIsDeviceObjInList");
645 OIC_LOG(ERROR, TAG, "[EDR][Native] remoteAddress is null");
649 jint length = u_arraylist_length(g_deviceStateList);
650 for (index = 0; index < length; index++)
653 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
656 OIC_LOG(DEBUG, TAG, "[EDR][Native] jarrayObj is null");
660 jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
663 OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_setAddress is null");
667 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
670 OIC_LOG(DEBUG, TAG, "[EDR][Native] setAddress is null");
674 if (!strcmp(remoteAddress, setAddress))
676 OIC_LOG(DEBUG, TAG, "the device is already set");
677 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
682 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
687 OIC_LOG(DEBUG, TAG, "there are no the Device obejct in list. we can add");
691 void CAEDRNativeSocketCloseToAll(JNIEnv *env)
693 OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRNativeSocketCloseToAll");
695 if (!g_deviceObjectList)
697 OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
701 jclass jni_cid_BTSocket = (*env)->FindClass(env, CLASSPATH_BT_SOCKET);
702 if (!jni_cid_BTSocket)
704 OIC_LOG(ERROR, TAG, "[EDR][Native] close: jni_cid_BTSocket is null");
708 jmethodID jni_mid_close = (*env)->GetMethodID(env, jni_cid_BTSocket, "close", "()V");
711 OIC_LOG(ERROR, TAG, "[EDR][Native] close: jni_mid_close is null");
716 jint length = u_arraylist_length(g_deviceStateList);
717 for (index = 0; index < length; index++)
719 jobject jni_obj_socket = (jobject) u_arraylist_get(g_deviceObjectList, index);
722 OIC_LOG(ERROR, TAG, "[EDR][Native] socket obj is null");
726 (*env)->CallVoidMethod(env, jni_obj_socket, jni_mid_close);
728 if ((*env)->ExceptionCheck(env))
730 OIC_LOG(ERROR, TAG, "[EDR][Native] close: close is Failed!!!");
731 (*env)->ExceptionDescribe(env);
732 (*env)->ExceptionClear(env);
738 void CAEDRNativeRemoveAllDeviceSocket(JNIEnv *env)
740 OIC_LOG(DEBUG, TAG, "CANativeRemoveAllDeviceObjsList");
742 if (!g_deviceObjectList)
744 OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
749 jint length = u_arraylist_length(g_deviceStateList);
750 for (index = 0; index < length; index++)
752 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
755 OIC_LOG(ERROR, TAG, "[EDR][Native] jarrayObj is null");
758 (*env)->DeleteGlobalRef(env, jarrayObj);
761 OICFree(g_deviceObjectList);
762 g_deviceObjectList = NULL;
766 void CAEDRNativeRemoveDeviceSocket(JNIEnv *env, jobject deviceSocket)
768 OIC_LOG(DEBUG, TAG, "CAEDRNativeRemoveDeviceSocket");
770 if (!g_deviceObjectList)
772 OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
777 jint length = u_arraylist_length(g_deviceStateList);
778 for (index = 0; index < length; index++)
780 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
783 OIC_LOG(DEBUG, TAG, "[EDR][Native] jarrayObj is null");
787 jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
790 OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_setAddress is null");
794 jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
795 if (!jni_remoteAddress)
797 OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_remoteAddress is null");
801 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
802 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
804 if (!strcmp(setAddress, remoteAddress))
806 OIC_LOG_V(DEBUG, TAG, "[EDR][Native] remove object : %s", remoteAddress);
807 (*env)->DeleteGlobalRef(env, jarrayObj);
808 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
809 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
811 CAEDRReorderingDeviceSocketList(index);
814 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
815 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
818 OIC_LOG(DEBUG, TAG, "[EDR][Native] there are no target object");
822 void CAEDRNativeRemoveDeviceSocketBaseAddr(JNIEnv *env, jstring address)
824 OIC_LOG(DEBUG, TAG, "CAEDRNativeRemoveDeviceSocket");
826 if (!g_deviceObjectList)
828 OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
833 jint length = u_arraylist_length(g_deviceStateList);
834 for (index = 0; index < length; index++)
836 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
839 OIC_LOG(DEBUG, TAG, "[EDR][Native] jarrayObj is null");
843 jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
846 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_setAddress is null");
849 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
850 const char* remoteAddress = (*env)->GetStringUTFChars(env, address, NULL);
852 if (!strcmp(setAddress, remoteAddress))
854 OIC_LOG_V(ERROR, TAG, "[EDR][Native] remove object : %s", remoteAddress);
855 (*env)->DeleteGlobalRef(env, jarrayObj);
856 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
857 (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
859 CAEDRReorderingDeviceSocketList(index);
862 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
863 (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
866 OIC_LOG(DEBUG, TAG, "[EDR][Native] there are no target object");
870 jobject CAEDRNativeGetDeviceSocket(uint32_t idx)
872 OIC_LOG(DEBUG, TAG, "CAEDRNativeGetDeviceSocket");
874 if (!g_deviceObjectList)
876 OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
880 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, idx);
883 OIC_LOG(ERROR, TAG, "[EDR][Native] jarrayObj is not available");
889 jobject CAEDRNativeGetDeviceSocketBaseAddr(JNIEnv *env, const char* remoteAddress)
891 OIC_LOG(DEBUG, TAG, "CAEDRNativeGetDeviceSocket");
893 if (!g_deviceObjectList)
895 OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
900 jint length = u_arraylist_length(g_deviceStateList);
901 for (index = 0; index < length; index++)
903 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
906 OIC_LOG(ERROR, TAG, "[EDR][Native] jarrayObj is null");
910 jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
913 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_setAddress is null");
916 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
918 if (!strcmp(setAddress, remoteAddress))
920 OIC_LOG_V(ERROR, TAG, "[EDR][Native] remove object : %s", remoteAddress);
921 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
924 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
930 uint32_t CAEDRGetSocketListLength()
932 if (!g_deviceObjectList)
934 OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
938 uint32_t length = u_arraylist_length(g_deviceObjectList);
943 void CAEDRReorderingDeviceSocketList(uint32_t index)
945 if (index >= g_deviceObjectList->length)
950 if (index < g_deviceObjectList->length - 1)
952 memmove(&g_deviceObjectList->data[index], &g_deviceObjectList->data[index + 1],
953 (g_deviceObjectList->length - index - 1) * sizeof(void *));
956 g_deviceObjectList->size--;
957 g_deviceObjectList->length--;