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