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