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