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