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