Fixed prevent and klock work issues reported.
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_edr_adapter / android / caedrutils.c
1 #include <jni.h>
2 #include <stdio.h>
3 #include <android/log.h>
4 #include "caedrutils.h"
5 #include "logger.h"
6 #include "oic_malloc.h"
7 #include "uthreadpool.h"
8 #include "uarraylist.h"
9
10 #define TAG PCF("CA_EDR_UTILS")
11
12 static const char *METHODID_OBJECTNONPARAM = "()Landroid/bluetooth/BluetoothAdapter;";
13 static const char *METHODID_STRINGNONPARAM = "()Ljava/lang/String;";
14 static const char *CLASSPATH_BT_ADPATER = "android/bluetooth/BluetoothAdapter";
15
16 static u_arraylist_t *gdeviceStateList = NULL;
17 static u_arraylist_t *gdeviceObjectList = NULL;
18
19 // get address from bluetooth socket
20 jstring CAEDRNativeGetAddressFromDeviceSocket(JNIEnv *env, jobject bluetoothSocketObj)
21 {
22     if(!bluetoothSocketObj)
23     {
24         return NULL;
25     }
26
27     jclass jni_cid_BTSocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
28     if(!jni_cid_BTSocket)
29     {
30         OIC_LOG_V(DEBUG, TAG, "[EDR] getRemoteAddress: jni_cid_BTSocket is null");
31         return NULL;
32     }
33
34     jmethodID jni_mid_getRemoteDevice = (*env)->GetMethodID(env, jni_cid_BTSocket, "getRemoteDevice",
35          "()Landroid/bluetooth/BluetoothDevice;");
36     if(!jni_mid_getRemoteDevice)
37     {
38         OIC_LOG_V(DEBUG, TAG, "[EDR] getRemoteAddress: jni_mid_getRemoteDevice is null");
39         return NULL;
40     }
41
42     jobject jni_obj_remoteBTDevice = (*env)->CallObjectMethod(env, bluetoothSocketObj, jni_mid_getRemoteDevice);
43     if(!jni_obj_remoteBTDevice)
44     {
45         OIC_LOG_V(DEBUG, TAG, "[EDR] getRemoteAddress: jni_obj_remoteBTDevice is null");
46         return NULL;
47     }
48
49     jclass jni_cid_BTDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
50     jmethodID j_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTDevice, "getAddress", "()Ljava/lang/String;");
51
52     jstring j_str_address = (*env)->CallObjectMethod(env, jni_obj_remoteBTDevice, j_mid_getAddress);
53     if(j_str_address)
54     {
55         const char * address = (*env)->GetStringUTFChars(env, j_str_address, NULL);
56         OIC_LOG_V(DEBUG, TAG, "[EDR] getRemoteAddress: ~~ remote device address is %s", address);
57 //      (*env)->ReleaseStringUTFChars(env, j_str_address, address);
58     } else {
59         return NULL;
60     }
61     return j_str_address;
62 }
63
64 jstring CAEDRNativeGetLocalDeviceAddress(JNIEnv* env)
65 {
66     jclass jni_cid_BTAdapter = (*env)->FindClass(env,  CLASSPATH_BT_ADPATER);
67     if(!jni_cid_BTAdapter)
68     {
69         OIC_LOG(DEBUG, TAG, "[EDR][Native] getAddress: jni_cid_BTAdapter is null");
70         return NULL;
71     }
72
73     jmethodID jni_mid_getDefaultAdapter =
74             (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
75     if(!jni_mid_getDefaultAdapter)
76     {
77         OIC_LOG(DEBUG, TAG, "[EDR][Native] getAddress: jni_mid_getDefaultAdapter is null");
78         return NULL;
79     }
80
81     jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTAdapter, "getAddress", METHODID_STRINGNONPARAM);
82     if(!jni_mid_getAddress)
83     {
84         OIC_LOG(DEBUG, TAG, "[EDR][Native] getAddress: jni_mid_getAddress is null");
85         return NULL;
86     }
87
88     jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
89     if(!jni_obj_BTAdapter)
90     {
91         OIC_LOG(DEBUG, TAG, "[EDR][Native] getAddress: jni_obj_BTAdapter is null");
92         return NULL;
93     }
94
95     jstring jni_str_address = (jstring)(*env)->CallObjectMethod(env, jni_obj_BTAdapter, jni_mid_getAddress);
96     if(!jni_str_address)
97     {
98         OIC_LOG(DEBUG, TAG, "[EDR][Native] getAddress: jni_str_address is null");
99         return NULL;
100     }
101
102     return jni_str_address;
103 }
104
105 jobjectArray CAEDRNativeGetBondedDevices(JNIEnv *env)
106 {
107     jclass jni_cid_BTAdapter = (*env)->FindClass(env,  CLASSPATH_BT_ADPATER);
108     if(!jni_cid_BTAdapter)
109     {
110         OIC_LOG(DEBUG, TAG, "[EDR][Native] getBondedDevices: jni_cid_BTAdapter is null");
111         return NULL;
112     }
113
114     jmethodID jni_mid_getDefaultAdapter =
115             (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
116
117     jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
118     if(!jni_obj_BTAdapter)
119     {
120         OIC_LOG(DEBUG, TAG, "[EDR][Native] getBondedDevices: bluetooth adapter is null");
121         return NULL;
122     }
123
124     // Get a list of currently paired devices
125     jmethodID jni_mid_getBondedDevices = (*env)->GetMethodID(env, jni_cid_BTAdapter,
126             "getBondedDevices", "()Ljava/util/Set;");
127     if(!jni_mid_getBondedDevices)
128     {
129         OIC_LOG(DEBUG, TAG, "[EDR][Native] getBondedDevices: jni_mid_getBondedDevicesr is null");
130         return NULL;
131     }
132
133     jobject jni_obj_setPairedDevices = (*env)->CallObjectMethod(env, jni_obj_BTAdapter, jni_mid_getBondedDevices);
134     if(!jni_obj_setPairedDevices)
135     {
136         OIC_LOG(DEBUG, TAG, "[EDR][Native] getBondedDevices: jni_obj_setPairedDevices is null");
137         return NULL;
138     }
139
140     // Convert the set to an object array
141     // object[] array = Set<BluetoothDevice>.toArray();
142     jclass jni_cid_Set = (*env)->FindClass(env,  "java/util/Set");
143     jmethodID jni_mid_toArray = (*env)->GetMethodID(env, jni_cid_Set, "toArray", "()[Ljava/lang/Object;");
144
145     if(!jni_mid_toArray)
146     {
147         OIC_LOG(DEBUG, TAG, "[EDR][Native] getBondedDevices: jni_mid_toArray is null");
148         return NULL;
149     }
150
151     jobjectArray jni_arrayPairedDevices = (jobjectArray)((*env)->CallObjectMethod(env,
152             jni_obj_setPairedDevices, jni_mid_toArray));
153     if(!jni_arrayPairedDevices)
154     {
155         OIC_LOG(DEBUG, TAG, "[EDR][Native] getBondedDevices: jni_arrayPairedDevices is null");
156         return NULL;
157     }
158
159     return jni_arrayPairedDevices;
160 }
161
162 jint CAEDRNativeGetBTStateOnInfo(JNIEnv *env)
163 {
164     jclass jni_cid_BTAdapter = (*env)->FindClass(env,  CLASSPATH_BT_ADPATER);
165     if(!jni_cid_BTAdapter)
166     {
167         OIC_LOG(DEBUG, TAG, "[EDR][Native] getBTStateOnInfo: jni_cid_BTAdapter is null");
168         return -1;
169     }
170
171     jfieldID jni_fid_stateon = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
172     if (jni_fid_stateon == 0)
173     {
174         OIC_LOG(DEBUG, TAG, "[EDR][Native] get_field_state is 0");
175         return -1;
176     }
177     jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, jni_fid_stateon);
178
179     OIC_LOG_V(DEBUG, TAG, "[EDR][Native] bluetooth STATE_ON state integer value : %d", jni_int_val);
180
181     return jni_int_val;
182 }
183
184 jboolean CAEDRNativeIsEnableBTAdapter(JNIEnv *env)
185 {
186     jclass jni_cid_BTAdapter = (*env)->FindClass(env,  CLASSPATH_BT_ADPATER);
187     if(!jni_cid_BTAdapter)
188     {
189         OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_cid_BTAdapter: jni_cid_BTAdapter is null");
190         return FALSE;
191     }
192
193     jmethodID jni_mid_getDefaultAdapter =
194             (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
195     if(!jni_mid_getDefaultAdapter)
196     {
197         OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_mid_getDefaultAdapter is null");
198         return FALSE;
199     }
200
201     jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
202     if(!jni_obj_BTAdapter)
203     {
204         OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_obj_BTAdapter is null");
205         return FALSE;
206     }
207
208     // isEnable()
209     jmethodID jni_mid_isEnable = (*env)->GetMethodID(env, jni_cid_BTAdapter, "isEnabled",
210             "()Z");
211     if(!jni_mid_isEnable)
212     {
213         OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_mid_isEnable is null");
214         return FALSE;
215     }
216
217     jboolean jni_isEnable = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_isEnable);
218     OIC_LOG_V(DEBUG, TAG, "[EDR][Native] adapter state is %d", jni_isEnable);
219
220     return jni_isEnable;
221 }
222
223 jstring CAEDRNativeGetAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice)
224 {
225     jclass jni_cid_device_list = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
226     if(!jni_cid_device_list)
227     {
228         OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_cid_device_list is null");
229         return NULL;
230     }
231
232     jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_device_list, "getAddress",
233             "()Ljava/lang/String;");
234     if(!jni_mid_getAddress)
235     {
236         OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_mid_getAddress is null");
237         return NULL;
238     }
239
240     jstring jni_address = (jstring)(*env)->CallObjectMethod(env, bluetoothDevice, jni_mid_getAddress);
241     if(!jni_address)
242     {
243         OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_address is null");
244         return NULL;
245     }
246     return jni_address;
247 }
248
249
250 /**
251  * BT State List
252  */
253 void CAEDRNativeCreateDeviceStateList()
254 {
255     OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRNativeCreateDeviceStateList");
256
257     // create new object array
258     if (gdeviceStateList == NULL)
259     {
260         OIC_LOG_V(DEBUG, TAG, "Create device list");
261
262         gdeviceStateList = u_arraylist_create();
263     }
264 }
265
266 void CAEDRUpdateDeviceState(uint32_t state, const char* address)
267 {
268     state_t *newstate = (state_t*) OICMalloc( sizeof(state_t) );
269     memset(newstate->address, 0, CA_MACADDR_SIZE);
270     strcpy(newstate->address, address);
271     newstate->state = state;
272
273     CAEDRNativeAddDeviceStateToList(newstate);
274 }
275
276 void CAEDRNativeAddDeviceStateToList(state_t* state)
277 {
278     if(!state)
279     {
280         OIC_LOG(DEBUG, TAG, "[EDR][Native] device is null");
281         return;
282     }
283
284     if(!gdeviceStateList)
285     {
286         OIC_LOG(DEBUG, TAG, "[EDR][Native] gdevice_list is null");
287         return;
288     }
289
290     if(CAEDRNativeIsDeviceInList(state->address)) {
291         CAEDRNativeRemoveDevice(state->address); // delete previous state for update new state
292     }
293     u_arraylist_add(gdeviceStateList, state);          // update new state
294     OIC_LOG_V(DEBUG, TAG, "Set State Info to List : %d", state->state);
295 }
296
297 jboolean CAEDRNativeIsDeviceInList(const char* remoteAddress){
298
299     jint index;
300     for (index = 0; index < u_arraylist_length(gdeviceStateList); index++)
301     {
302         state_t* state = (state_t*) u_arraylist_get(gdeviceStateList, index);
303         if(!state)
304         {
305             OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
306             return TRUE;
307         }
308
309         if(!strcmp(remoteAddress, state->address))
310         {
311             OIC_LOG_V(DEBUG, TAG, "the device is already set");
312             return TRUE;
313         }
314         else
315         {
316             continue;
317         }
318     }
319
320     OIC_LOG_V(DEBUG, TAG, "there are no the device in list.");
321     return FALSE;
322 }
323
324 void CAEDRNativeRemoveAllDeviceState()
325 {
326     OIC_LOG_V(DEBUG, TAG, "CAEDRNativeRemoveAllDevices");
327
328     if(!gdeviceStateList)
329     {
330         OIC_LOG(DEBUG, TAG, "[EDR][Native] gdeviceStateList is null");
331         return;
332     }
333
334     jint index;
335     for (index = 0; index < u_arraylist_length(gdeviceStateList); index++)
336     {
337         state_t* state = (state_t*) u_arraylist_get(gdeviceStateList, index);
338         if(!state)
339         {
340             OIC_LOG(DEBUG, TAG, "[EDR][Native] jarrayObj is null");
341             continue;
342         }
343         OICFree(state);
344     }
345
346     OICFree(gdeviceStateList);
347     gdeviceStateList = NULL;
348     return;
349 }
350
351 void CAEDRNativeRemoveDevice(const char* remoteAddress)
352 {
353     OIC_LOG_V(DEBUG, TAG, "CAEDRNativeRemoveDeviceforStateList");
354
355     if(!gdeviceStateList)
356     {
357         OIC_LOG(DEBUG, TAG, "[EDR][Native] gdeviceStateList is null");
358         return;
359     }
360
361     jint index;
362     for (index = 0; index < u_arraylist_length(gdeviceStateList); index++)
363     {
364         state_t* state = (state_t*) u_arraylist_get(gdeviceStateList, index);
365         if(!state)
366         {
367             OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
368             continue;
369         }
370
371         if(!strcmp(state->address, remoteAddress))
372         {
373             OIC_LOG_V(DEBUG, TAG, "[EDR][Native] remove state : %s", remoteAddress);
374             OICFree(state);
375
376             CAEDRReorderingDeviceList(index);
377             break;
378         }
379     }
380     return;
381 }
382
383 jboolean CAEDRIsConnectedDevice(const char* remoteAddress)
384 {
385     OIC_LOG_V(DEBUG, TAG, "CAEDRIsConnectedDevice");
386
387     if(!gdeviceStateList)
388     {
389         OIC_LOG(DEBUG, TAG, "[EDR][Native] gdeviceStateList is null");
390         return FALSE;
391     }
392
393     jint index;
394     for (index = 0; index < u_arraylist_length(gdeviceStateList); index++)
395     {
396         state_t* state = (state_t*) u_arraylist_get(gdeviceStateList, index);
397         if(!state)
398         {
399             OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
400             continue;
401         }
402
403         if(!strcmp(state->address, remoteAddress))
404         {
405             OIC_LOG_V(DEBUG, TAG, "[EDR][Native] check whether it is connected or not");
406
407             return state->state;
408         }
409     }
410     return FALSE;
411 }
412
413 void CAEDRReorderingDeviceList(uint32_t index)
414 {
415     if (index >= gdeviceStateList->length)
416     {
417         return;
418     }
419
420     if (index < gdeviceStateList->length - 1)
421     {
422         memmove(&gdeviceStateList->data[index], &gdeviceStateList->data[index + 1],
423                 (gdeviceStateList->length - index - 1) * sizeof(void *));
424     }
425
426     gdeviceStateList->size--;
427     gdeviceStateList->length--;
428 }
429
430 /**
431  * Device Socket Object List
432  */
433 void CAEDRNativeCreateDeviceSocketList()
434 {
435     OIC_LOG(DEBUG, TAG, "[BLE][Native] CAEDRNativeCreateDeviceSocketList");
436
437     // create new object array
438     if (gdeviceObjectList == NULL)
439     {
440         OIC_LOG_V(DEBUG, TAG, "Create Device object list");
441
442         gdeviceObjectList = u_arraylist_create();
443     }
444 }
445
446 void CAEDRNativeAddDeviceSocketToList(JNIEnv *env, jobject deviceSocket)
447 {
448     OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeAddDeviceobjToList");
449
450     if(!deviceSocket)
451     {
452         OIC_LOG(DEBUG, TAG, "[BLE][Native] Device is null");
453         return;
454     }
455
456     if(!gdeviceObjectList)
457     {
458         OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
459         return;
460     }
461
462     jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
463     if(!jni_remoteAddress)
464     {
465         OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
466         return;
467     }
468
469     const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
470
471     if(!CAEDRNativeIsDeviceSocketInList(env, remoteAddress))
472     {
473         jobject gDeviceSocker = (*env)->NewGlobalRef(env, deviceSocket);
474         u_arraylist_add(gdeviceObjectList, gDeviceSocker);
475         OIC_LOG_V(DEBUG, TAG, "Set Socket Object to Array");
476     }
477 }
478
479 jboolean CAEDRNativeIsDeviceSocketInList(JNIEnv *env, const char* remoteAddress)
480 {
481     OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeIsDeviceObjInList");
482
483     jint index;
484     for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
485     {
486
487         jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, index);
488         if(!jarrayObj)
489         {
490             OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
491             return TRUE;
492         }
493
494         jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
495         if(!jni_setAddress)
496         {
497             OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
498             return TRUE;
499         }
500
501         const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
502
503         if(!strcmp(remoteAddress, setAddress))
504         {
505             OIC_LOG_V(DEBUG, TAG, "the device is already set");
506             return TRUE;
507         }
508         else
509         {
510             continue;
511         }
512     }
513
514     OIC_LOG_V(DEBUG, TAG, "there are no the Device obejct in list. we can add");
515     return FALSE;
516 }
517
518 void CAEDRNativeSocketCloseToAll(JNIEnv *env)
519 {
520     OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRNativeSocketCloseToAll");
521
522     if(!gdeviceObjectList)
523     {
524         OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
525         return;
526     }
527
528     jclass jni_cid_BTSocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
529     if(!jni_cid_BTSocket)
530     {
531         OIC_LOG(DEBUG, TAG, "[EDR][Native] close: jni_cid_BTSocket is null");
532         return;
533     }
534
535     jmethodID jni_mid_close = (*env)->GetMethodID(env, jni_cid_BTSocket, "close", "()V");
536     if(!jni_mid_close)
537     {
538         OIC_LOG(DEBUG, TAG, "[EDR][Native] close: jni_mid_close is null");
539         return;
540     }
541
542     jint index;
543     for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
544     {
545         jobject jni_obj_socket = (jobject) u_arraylist_get(gdeviceObjectList, index);
546         if(!jni_obj_socket)
547         {
548             OIC_LOG(DEBUG, TAG, "[BLE][Native] socket obj is null");
549             return;
550         }
551
552         (*env)->CallVoidMethod(env, jni_obj_socket, jni_mid_close);
553
554         if((*env)->ExceptionCheck(env))
555         {
556             OIC_LOG(DEBUG, TAG, "[EDR][Native] close: close is Failed!!!");
557             (*env)->ExceptionDescribe(env);
558             (*env)->ExceptionClear(env);
559             return;
560         }
561     }
562 }
563
564 void CAEDRNativeRemoveAllDeviceSocket(JNIEnv *env)
565 {
566     OIC_LOG_V(DEBUG, TAG, "CANativeRemoveAllDeviceObjsList");
567
568     if(!gdeviceObjectList)
569     {
570         OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
571         return;
572     }
573
574     jint index;
575     for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
576     {
577         jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, index);
578         if(!jarrayObj)
579         {
580             OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
581             return;
582         }
583         (*env)->DeleteGlobalRef(env, jarrayObj);
584     }
585
586     OICFree(gdeviceObjectList);
587     gdeviceObjectList = NULL;
588     return;
589 }
590
591 void CAEDRNativeRemoveDeviceSocket(JNIEnv *env, jobject deviceSocket)
592 {
593     OIC_LOG_V(DEBUG, TAG, "CAEDRNativeRemoveDeviceSocket");
594
595     if(!gdeviceObjectList)
596     {
597         OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
598         return;
599     }
600
601     jint index;
602     for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
603     {
604         jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, index);
605         if(!jarrayObj)
606         {
607             OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
608             continue;
609         }
610
611         jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
612         if(!jni_setAddress)
613         {
614             OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
615             continue;
616         }
617         const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
618
619         jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
620         if(!jni_remoteAddress)
621         {
622             OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
623             continue;
624         }
625         const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
626
627         if(!strcmp(setAddress, remoteAddress))
628         {
629             OIC_LOG_V(DEBUG, TAG, "[BLE][Native] remove object : %s", remoteAddress);
630             (*env)->DeleteGlobalRef(env, jarrayObj);
631
632             CAEDRReorderingDeviceSocketList(index);
633             break;
634         }
635     }
636
637     OIC_LOG(DEBUG, TAG, "[BLE][Native] there are no target object");
638     return;
639 }
640
641 void CAEDRNativeRemoveDeviceSocketBaseAddr(JNIEnv *env, jstring address)
642 {
643     OIC_LOG_V(DEBUG, TAG, "CAEDRNativeRemoveDeviceSocket");
644
645     if(!gdeviceObjectList)
646     {
647         OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
648         return;
649     }
650
651     jint index;
652     for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
653     {
654         jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, index);
655         if(!jarrayObj)
656         {
657             OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
658             continue;
659         }
660
661         jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
662         if(!jni_setAddress)
663         {
664             OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
665             continue;
666         }
667         const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
668         const char* remoteAddress = (*env)->GetStringUTFChars(env, address, NULL);
669
670         if(!strcmp(setAddress, remoteAddress))
671         {
672             OIC_LOG_V(DEBUG, TAG, "[BLE][Native] remove object : %s", remoteAddress);
673             (*env)->DeleteGlobalRef(env, jarrayObj);
674
675             CAEDRReorderingDeviceSocketList(index);
676             break;
677         }
678     }
679
680     OIC_LOG(DEBUG, TAG, "[BLE][Native] there are no target object");
681     return;
682 }
683
684 jobject CAEDRNativeGetDeviceSocket(uint32_t idx)
685 {
686     OIC_LOG_V(DEBUG, TAG, "CAEDRNativeGetDeviceSocket");
687
688     if(!gdeviceObjectList)
689     {
690         OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
691         return NULL;
692     }
693
694     jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, idx);
695     if(!jarrayObj)
696     {
697         OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is not available");
698         return NULL;
699     }
700     return jarrayObj;
701 }
702
703 uint32_t CAEDRGetSocketListLength()
704 {
705     if(!gdeviceObjectList)
706     {
707         OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
708         return 0;
709     }
710
711     uint32_t length = u_arraylist_length(gdeviceObjectList);
712
713     return length;
714 }
715
716 void CAEDRReorderingDeviceSocketList(uint32_t index)
717 {
718     if (index >= gdeviceObjectList->length)
719     {
720         return;
721     }
722
723     if (index < gdeviceObjectList->length - 1)
724     {
725         memmove(&gdeviceObjectList->data[index], &gdeviceObjectList->data[index + 1],
726                 (gdeviceObjectList->length - index - 1) * sizeof(void *));
727     }
728
729     gdeviceObjectList->size--;
730     gdeviceObjectList->length--;
731 }