Changed to be used only once to create input stream.
[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         CAEDRSocketInfo_t *socketInfo = (CAEDRSocketInfo_t *) OICCalloc(1, sizeof(*socketInfo));
562         if (!socketInfo)
563         {
564             OIC_LOG(ERROR, TAG, "Out of memory");
565             return;
566         }
567
568         jmethodID jni_mid_getInputStream = CAGetJNIMethodID(env,
569                                                             "android/bluetooth/BluetoothSocket",
570                                                             "getInputStream",
571                                                             "()Ljava/io/InputStream;");
572         if (!jni_mid_getInputStream)
573         {
574             OIC_LOG(ERROR, TAG, "jni_mid_getInputStream is null");
575             return;
576         }
577
578         jobject jni_obj_inputStream = (*env)->CallObjectMethod(env, deviceSocket,
579                                                                jni_mid_getInputStream);
580         if (!jni_obj_inputStream)
581         {
582             OIC_LOG(ERROR, TAG, "jni_obj_inputStream is null");
583             return;
584         }
585
586         socketInfo->deviceSocket = (*env)->NewGlobalRef(env, deviceSocket);
587         socketInfo->inputStream = (*env)->NewGlobalRef(env, jni_obj_inputStream);
588         (*env)->DeleteLocalRef(env, jni_obj_inputStream);
589
590         bool result = u_arraylist_add(g_deviceObjectList, (void *) socketInfo);
591         if (!result)
592         {
593             OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
594             OICFree(socketInfo);
595             return;
596         }
597
598         OIC_LOG(DEBUG, TAG, "add new device socket object to list");
599     }
600     (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
601     (*env)->DeleteLocalRef(env, jni_remoteAddress);
602 }
603
604 bool CAEDRNativeIsDeviceSocketInList(JNIEnv *env, const char* remoteAddress)
605 {
606     OIC_LOG(DEBUG, TAG, "CANativeIsDeviceObjInList");
607
608     if (!remoteAddress)
609     {
610         OIC_LOG(ERROR, TAG, "remoteAddress is null");
611         return false;
612     }
613
614     jint length = u_arraylist_length(g_deviceStateList);
615     for (jint index = 0; index < length; index++)
616     {
617         CAEDRSocketInfo_t *socketInfo = (CAEDRSocketInfo_t *) u_arraylist_get(g_deviceObjectList,
618                                                                               index);
619         if (!socketInfo)
620         {
621             OIC_LOG(DEBUG, TAG, "socketInfo is null");
622             return false;
623         }
624
625         jobject jarrayObj = socketInfo->deviceSocket;
626         if (!jarrayObj)
627         {
628             OIC_LOG(DEBUG, TAG, "jarrayObj is null");
629             return false;
630         }
631
632         jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
633         if (!jni_setAddress)
634         {
635             OIC_LOG(DEBUG, TAG, "jni_setAddress is null");
636             return false;
637         }
638
639         const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
640         if (!setAddress)
641         {
642             OIC_LOG(DEBUG, TAG, "setAddress is null");
643             return false;
644         }
645
646         if (!strcmp(remoteAddress, setAddress))
647         {
648             OIC_LOG(DEBUG, TAG, "the device is already set");
649             (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
650             return true;
651         }
652         else
653         {
654             (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
655             continue;
656         }
657     }
658
659     OIC_LOG(DEBUG, TAG, "there are no the Device obejct in list");
660     return false;
661 }
662
663 void CAEDRNativeSocketCloseToAll(JNIEnv *env)
664 {
665     OIC_LOG(DEBUG, TAG, "CAEDRNativeSocketCloseToAll");
666
667     if (!g_deviceObjectList)
668     {
669         OIC_LOG(ERROR, TAG, "gdeviceObjectList is null");
670         return;
671     }
672
673     jmethodID jni_mid_close = CAGetJNIMethodID(env, CLASSPATH_BT_SOCKET,
674                                                "close", "()V");
675     if (!jni_mid_close)
676     {
677         OIC_LOG(ERROR, TAG, "jni_mid_close is null");
678         return;
679     }
680
681     jint length = u_arraylist_length(g_deviceStateList);
682     for (jint index = 0; index < length; index++)
683     {
684         jobject jni_obj_socket = (jobject) u_arraylist_get(g_deviceObjectList, index);
685         if (!jni_obj_socket)
686         {
687             OIC_LOG(ERROR, TAG, "socket obj is null");
688             return;
689         }
690
691         (*env)->CallVoidMethod(env, jni_obj_socket, jni_mid_close);
692
693         if ((*env)->ExceptionCheck(env))
694         {
695             OIC_LOG(ERROR, TAG, "close is Failed!!!");
696             (*env)->ExceptionDescribe(env);
697             (*env)->ExceptionClear(env);
698             return;
699         }
700     }
701 }
702
703 void CAEDRNativeRemoveAllDeviceSocket(JNIEnv *env)
704 {
705     OIC_LOG(DEBUG, TAG, "CANativeRemoveAllDeviceObjsList");
706
707     if (!g_deviceObjectList)
708     {
709         OIC_LOG(ERROR, TAG, "gdeviceObjectList is null");
710         return;
711     }
712
713     jint length = u_arraylist_length(g_deviceStateList);
714     for (jint index = 0; index < length; index++)
715     {
716
717         CAEDRSocketInfo_t *socketInfo = (CAEDRSocketInfo_t *) u_arraylist_get(g_deviceObjectList,
718                                                                               index);
719         if (!socketInfo)
720         {
721             OIC_LOG(ERROR, TAG, "socketInfo is null");
722             continue;
723         }
724
725         jobject jdeviceSocket = socketInfo->deviceSocket;
726         if (jdeviceSocket)
727         {
728             (*env)->DeleteGlobalRef(env, jdeviceSocket);
729         }
730
731         jobject jinputStream = socketInfo->inputStream;
732         if (jinputStream)
733         {
734             (*env)->DeleteGlobalRef(env, jinputStream);
735         }
736     }
737
738     OICFree(g_deviceObjectList);
739     g_deviceObjectList = NULL;
740     return;
741 }
742
743 void CAEDRNativeRemoveDeviceSocket(JNIEnv *env, jobject deviceSocket)
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         CAEDRSocketInfo_t *socketInfo = (CAEDRSocketInfo_t *) u_arraylist_get(g_deviceObjectList,
757                                                                               index);
758         if (!socketInfo)
759         {
760             OIC_LOG(ERROR, TAG, "socketInfo is null");
761             continue;
762         }
763
764         jobject jarrayObj = socketInfo->deviceSocket;
765         if (!jarrayObj)
766         {
767             OIC_LOG(DEBUG, TAG, "jarrayObj is null");
768             continue;
769         }
770
771         jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
772         if (!jni_setAddress)
773         {
774             OIC_LOG(DEBUG, TAG, "jni_setAddress is null");
775             continue;
776         }
777
778         jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
779         if (!jni_remoteAddress)
780         {
781             OIC_LOG(DEBUG, TAG, "jni_remoteAddress is null");
782             continue;
783         }
784
785         const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
786         const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
787
788         if (!strcmp(setAddress, remoteAddress))
789         {
790             OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
791             (*env)->DeleteGlobalRef(env, jarrayObj);
792             jobject jinputStream = socketInfo->inputStream;
793             if (jinputStream)
794             {
795                 (*env)->DeleteGlobalRef(env, jinputStream);
796             }
797             (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
798             (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
799
800             u_arraylist_remove(g_deviceObjectList, index);
801             break;
802         }
803         (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
804         (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
805     }
806
807     OIC_LOG(DEBUG, TAG, "there are no target object");
808     return;
809 }
810
811 void CAEDRNativeRemoveDeviceSocketBaseAddr(JNIEnv *env, jstring address)
812 {
813     OIC_LOG(DEBUG, TAG, "CAEDRNativeRemoveDeviceSocket");
814
815     if (!g_deviceObjectList)
816     {
817         OIC_LOG(ERROR, TAG, "gdeviceObjectList is null");
818         return;
819     }
820
821     jint length = u_arraylist_length(g_deviceStateList);
822     for (jint index = 0; index < length; index++)
823     {
824         CAEDRSocketInfo_t *socketInfo = (CAEDRSocketInfo_t *) u_arraylist_get(g_deviceObjectList,
825                                                                               index);
826         if (!socketInfo)
827         {
828             OIC_LOG(ERROR, TAG, "socketInfo is null");
829             continue;
830         }
831
832         jobject jarrayObj = socketInfo->deviceSocket;
833         if (!jarrayObj)
834         {
835             OIC_LOG(DEBUG, TAG, "jarrayObj is null");
836             continue;
837         }
838
839         jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
840         if (!jni_setAddress)
841         {
842             OIC_LOG(ERROR, TAG, "jni_setAddress is null");
843             continue;
844         }
845         const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
846         const char* remoteAddress = (*env)->GetStringUTFChars(env, address, NULL);
847
848         if (!strcmp(setAddress, remoteAddress))
849         {
850             OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
851             (*env)->DeleteGlobalRef(env, jarrayObj);
852             jobject jinputStream = socketInfo->inputStream;
853             if (jinputStream)
854             {
855                 (*env)->DeleteGlobalRef(env, jinputStream);
856             }
857             (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
858             (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
859
860             u_arraylist_remove(g_deviceObjectList, index);
861             break;
862         }
863         (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
864         (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
865     }
866
867     OIC_LOG(DEBUG, TAG, "there are no target object");
868     return;
869 }
870
871 jobject CAEDRNativeGetDeviceSocket(uint32_t index)
872 {
873     if (!g_deviceObjectList)
874     {
875         OIC_LOG(ERROR, TAG, "gdeviceObjectList is null");
876         return NULL;
877     }
878
879     CAEDRSocketInfo_t *socketInfo = (CAEDRSocketInfo_t *) u_arraylist_get(g_deviceObjectList,
880                                                                           index);
881     if (!socketInfo)
882     {
883         OIC_LOG(ERROR, TAG, "socketInfo is null");
884         return NULL;
885     }
886
887     jobject jarrayObj = socketInfo->deviceSocket;
888     if (!jarrayObj)
889     {
890         OIC_LOG(ERROR, TAG, "jarrayObj is not available");
891         return NULL;
892     }
893     return jarrayObj;
894 }
895
896 jobject CAEDRNativeGetDeviceSocketBaseAddr(JNIEnv *env, const char* remoteAddress)
897 {
898     OIC_LOG(DEBUG, TAG, "CAEDRNativeGetDeviceSocket");
899
900     if (!g_deviceObjectList)
901     {
902         OIC_LOG(ERROR, TAG, "gdeviceObjectList is null");
903         return NULL;
904     }
905
906     jint length = u_arraylist_length(g_deviceStateList);
907     for (jint index = 0; index < length; index++)
908     {
909         CAEDRSocketInfo_t *socketInfo = (CAEDRSocketInfo_t *) u_arraylist_get(g_deviceObjectList,
910                                                                               index);
911         if (!socketInfo)
912         {
913             OIC_LOG(ERROR, TAG, "socketInfo is null");
914             continue;
915         }
916
917         jobject jarrayObj = socketInfo->deviceSocket;
918         if (!jarrayObj)
919         {
920             OIC_LOG(ERROR, TAG, "jarrayObj is null");
921             continue;
922         }
923
924         jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
925         if (!jni_setAddress)
926         {
927             OIC_LOG(ERROR, TAG, "jni_setAddress is null");
928             continue;
929         }
930         const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
931
932         if (!strcmp(setAddress, remoteAddress))
933         {
934             OIC_LOG_V(ERROR, TAG, "remove object : %s", remoteAddress);
935             (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
936             (*env)->DeleteLocalRef(env, jni_setAddress);
937             return jarrayObj;
938         }
939         (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
940         (*env)->DeleteLocalRef(env, jni_setAddress);
941     }
942
943     return NULL;
944 }
945
946 jobject CAEDRNativeGetInputStream(uint32_t index)
947 {
948     if (!g_deviceObjectList)
949     {
950         OIC_LOG(ERROR, TAG, "gdeviceObjectList is null");
951         return NULL;
952     }
953
954     CAEDRSocketInfo_t *socketInfo = (CAEDRSocketInfo_t *) u_arraylist_get(g_deviceObjectList,
955                                                                           index);
956     if (!socketInfo)
957     {
958         OIC_LOG(ERROR, TAG, "socketInfo is null");
959         return NULL;
960     }
961
962     jobject jarrayObj = socketInfo->inputStream;
963     if (!jarrayObj)
964     {
965         OIC_LOG(ERROR, TAG, "jarrayObj is not available");
966         return NULL;
967     }
968     return jarrayObj;
969 }
970
971 uint32_t CAEDRGetSocketListLength()
972 {
973     if (!g_deviceObjectList)
974     {
975         OIC_LOG(ERROR, TAG, "gdeviceObjectList is null");
976         return 0;
977     }
978
979     return u_arraylist_length(g_deviceObjectList);
980 }
981
982 CAConnectedDeviceInfo_t *CAEDRGetDeviceInfoFromAddress(const char *remoteAddress)
983 {
984     OIC_LOG(DEBUG, TAG, "CAEDRGetDeviceInfoFromAddress");
985
986     if (!g_deviceStateList)
987     {
988         OIC_LOG(ERROR, TAG, "gdeviceStateList is null");
989         return NULL;
990     }
991     if (!remoteAddress)
992     {
993         OIC_LOG(ERROR, TAG, "remoteAddress is null");
994         return NULL;
995     }
996
997     jint length = u_arraylist_length(g_deviceStateList);
998     for (jint index = 0; index < length; index++)
999     {
1000         CAConnectedDeviceInfo_t* deviceInfo =
1001                 (CAConnectedDeviceInfo_t*) u_arraylist_get(g_deviceStateList, index);
1002         if (!deviceInfo)
1003         {
1004             OIC_LOG(DEBUG, TAG, "deviceInfo object is null");
1005             continue;
1006         }
1007
1008         if (!strcmp((const char*) deviceInfo->address, remoteAddress))
1009         {
1010             return deviceInfo;
1011         }
1012     }
1013     return NULL;
1014 }