Corrected direct use of u_arraylist_t internals.
[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
237         OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: jni_cid_Set is null");
238         return NULL;
239     }
240     jmethodID jni_mid_toArray = (*env)->GetMethodID(env, jni_cid_Set, "toArray",
241                                                     "()[Ljava/lang/Object;");
242
243     if (!jni_mid_toArray)
244     {
245         (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
246         (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
247
248         OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: jni_mid_toArray is null");
249         return NULL;
250     }
251
252     jobjectArray jni_arrayPairedDevices = (jobjectArray)(
253             (*env)->CallObjectMethod(env, jni_obj_setPairedDevices, jni_mid_toArray));
254     if (!jni_arrayPairedDevices)
255     {
256         (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
257         (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
258
259         OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: jni_arrayPairedDevices is null");
260         return NULL;
261     }
262
263     (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
264     (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
265
266     return jni_arrayPairedDevices;
267 }
268
269 jint CAEDRNativeGetBTStateOnInfo(JNIEnv *env)
270 {
271     jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
272     if (!jni_cid_BTAdapter)
273     {
274         OIC_LOG(ERROR, TAG, "[EDR][Native] getBTStateOnInfo: jni_cid_BTAdapter is null");
275         return ERROR_CODE;
276     }
277
278     jfieldID jni_fid_stateon = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
279     if (jni_fid_stateon == 0)
280     {
281         (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
282
283         OIC_LOG(ERROR, TAG, "[EDR][Native] get_field_state is 0");
284         return ERROR_CODE;
285     }
286     jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, jni_fid_stateon);
287
288     OIC_LOG_V(DEBUG, TAG, "[EDR][Native] bluetooth state integer value : %d", jni_int_val);
289
290     (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
291
292     return jni_int_val;
293 }
294
295 jboolean CAEDRNativeIsEnableBTAdapter(JNIEnv *env)
296 {
297     jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
298     if (!jni_cid_BTAdapter)
299     {
300         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_cid_BTAdapter: jni_cid_BTAdapter is null");
301         return JNI_FALSE;
302     }
303
304     jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
305                                                                     "getDefaultAdapter",
306                                                                     METHODID_OBJECTNONPARAM);
307     if (!jni_mid_getDefaultAdapter)
308     {
309         (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
310
311         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_mid_getDefaultAdapter is null");
312         return JNI_FALSE;
313     }
314
315     jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
316                                                                jni_mid_getDefaultAdapter);
317     if (!jni_obj_BTAdapter)
318     {
319         (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
320
321         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_obj_BTAdapter is null");
322         return JNI_FALSE;
323     }
324
325     // isEnable()
326     jmethodID jni_mid_isEnable = (*env)->GetMethodID(env, jni_cid_BTAdapter, "isEnabled", "()Z");
327     if (!jni_mid_isEnable)
328     {
329         (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
330         (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
331
332         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_mid_isEnable is null");
333         return JNI_FALSE;
334     }
335
336     jboolean jni_isEnable = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_isEnable);
337     OIC_LOG_V(DEBUG, TAG, "[EDR][Native] adapter state is %d", jni_isEnable);
338
339     (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
340     (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
341
342     return jni_isEnable;
343 }
344
345 jstring CAEDRNativeGetAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice)
346 {
347     if (!bluetoothDevice)
348     {
349         OIC_LOG(ERROR, TAG, "[EDR][Native] bluetoothDevice is null");
350         return NULL;
351     }
352     jclass jni_cid_device_list = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
353     if (!jni_cid_device_list)
354     {
355         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_cid_device_list is null");
356         return NULL;
357     }
358
359     jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_device_list, "getAddress",
360                                                        METHODID_STRINGNONPARAM);
361     if (!jni_mid_getAddress)
362     {
363         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_mid_getAddress is null");
364         return NULL;
365     }
366
367     jstring jni_address = (jstring)(*env)->CallObjectMethod(env, bluetoothDevice,
368                                                             jni_mid_getAddress);
369     if (!jni_address)
370     {
371         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_address is null");
372         return NULL;
373     }
374     return jni_address;
375 }
376
377 /**
378  * BT State List
379  */
380 void CAEDRNativeCreateDeviceStateList()
381 {
382     OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRNativeCreateDeviceStateList");
383
384     // create new object array
385     if (NULL == g_deviceStateList)
386     {
387         OIC_LOG(DEBUG, TAG, "Create device list");
388
389         g_deviceStateList = u_arraylist_create();
390     }
391 }
392
393 void CAEDRUpdateDeviceState(CAConnectedState_t state, const char *address)
394 {
395     if (!address)
396     {
397         OIC_LOG(ERROR, TAG, "[EDR][Native] address is null");
398         return;
399     }
400     state_t *newstate = (state_t*) OICCalloc(1, sizeof(state_t));
401     if (!newstate)
402     {
403         OIC_LOG(ERROR, TAG, "[EDR][Native] newstate is null");
404         return;
405     }
406     OICStrcpy((char*) newstate->address, sizeof(newstate->address), address);
407     newstate->state = state;
408
409     CAEDRNativeAddDeviceStateToList(newstate);
410 }
411
412 void CAEDRNativeAddDeviceStateToList(state_t *state)
413 {
414     if (!state)
415     {
416         OIC_LOG(ERROR, TAG, "[EDR][Native] device is null");
417         return;
418     }
419
420     if (!g_deviceStateList)
421     {
422         OIC_LOG(ERROR, TAG, "[EDR][Native] gdevice_list is null");
423         return;
424     }
425
426     if (CAEDRNativeIsDeviceInList((const char*) state->address))
427     {
428         // delete previous state for update new state
429         CAEDRNativeRemoveDevice((const char*) state->address);
430     }
431     u_arraylist_add(g_deviceStateList, state); // update new state
432     OIC_LOG_V(DEBUG, TAG, "Set State Info to List : %d", state->state);
433 }
434
435 bool CAEDRNativeIsDeviceInList(const char* remoteAddress)
436 {
437
438     if (!remoteAddress)
439     {
440         OIC_LOG(ERROR, TAG, "[EDR][Native] remoteAddress is null");
441         return false;
442     }
443     jint index;
444     jint length = u_arraylist_length(g_deviceStateList);
445     for (index = 0; index < length; index++)
446     {
447         state_t* state = (state_t*) u_arraylist_get(g_deviceStateList, index);
448         if (!state)
449         {
450             OIC_LOG(ERROR, TAG, "[EDR][Native] state_t object is null");
451             return false;
452         }
453
454         if (!strcmp(remoteAddress, (const char*) state->address))
455         {
456             OIC_LOG(DEBUG, TAG, "the device is already set");
457             return true;
458         }
459         else
460         {
461             continue;
462         }
463     }
464
465     OIC_LOG(DEBUG, TAG, "there are no the device in list.");
466     return false;
467 }
468
469 void CAEDRNativeRemoveAllDeviceState()
470 {
471     OIC_LOG(DEBUG, TAG, "CAEDRNativeRemoveAllDevices");
472
473     if (!g_deviceStateList)
474     {
475         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceStateList is null");
476         return;
477     }
478
479     jint index;
480     jint length = u_arraylist_length(g_deviceStateList);
481     for (index = 0; index < length; index++)
482     {
483         state_t* state = (state_t*) u_arraylist_get(g_deviceStateList, index);
484         if (!state)
485         {
486             OIC_LOG(DEBUG, TAG, "[EDR][Native] jarrayObj is null");
487             continue;
488         }
489         OICFree(state);
490     }
491
492     OICFree(g_deviceStateList);
493     g_deviceStateList = NULL;
494     return;
495 }
496
497 void CAEDRNativeRemoveDevice(const char *remoteAddress)
498 {
499     OIC_LOG(DEBUG, TAG, "CAEDRNativeRemoveDeviceforStateList");
500
501     if (!g_deviceStateList)
502     {
503         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceStateList is null");
504         return;
505     }
506     if (!remoteAddress)
507     {
508         OIC_LOG(ERROR, TAG, "[EDR][Native] remoteAddress is null");
509         return;
510     }
511
512     jint index;
513     jint length = u_arraylist_length(g_deviceStateList);
514     for (index = 0; index < length; index++)
515     {
516         state_t* state = (state_t*) u_arraylist_get(g_deviceStateList, index);
517         if (!state)
518         {
519             OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
520             continue;
521         }
522
523         if (!strcmp((const char*) state->address, remoteAddress))
524         {
525             OIC_LOG_V(DEBUG, TAG, "[EDR][Native] remove state : %s", remoteAddress);
526             OICFree(state);
527
528             CAEDRReorderingDeviceList(index);
529             break;
530         }
531     }
532     return;
533 }
534
535 CAConnectedState_t CAEDRIsConnectedDevice(const char *remoteAddress)
536 {
537     OIC_LOG(DEBUG, TAG, "CAEDRIsConnectedDevice");
538
539     if (!remoteAddress)
540     {
541         OIC_LOG(ERROR, TAG, "[EDR][Native] remoteAddress is null");
542         return STATE_DISCONNECTED;
543     }
544
545     if (!g_deviceStateList)
546     {
547         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceStateList is null");
548         return STATE_DISCONNECTED;
549     }
550
551     jint index;
552     jint length = u_arraylist_length(g_deviceStateList);
553     for (index = 0; index < length; index++)
554     {
555         state_t* state = (state_t*) u_arraylist_get(g_deviceStateList, index);
556         if (!state)
557         {
558             OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
559             continue;
560         }
561
562         if (!strcmp((const char*) state->address, remoteAddress))
563         {
564             OIC_LOG(DEBUG, TAG, "[EDR][Native] check whether it is connected or not");
565
566             return state->state;
567         }
568     }
569     return STATE_DISCONNECTED;
570 }
571
572 void CAEDRReorderingDeviceList(uint32_t index)
573 {
574     u_arraylist_remove(g_deviceStateList, index);
575 }
576
577 /**
578  * Device Socket Object List
579  */
580 void CAEDRNativeCreateDeviceSocketList()
581 {
582     OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRNativeCreateDeviceSocketList");
583
584     // create new object array
585     if (NULL == g_deviceObjectList)
586     {
587         OIC_LOG(DEBUG, TAG, "Create Device object list");
588
589         g_deviceObjectList = u_arraylist_create();
590     }
591 }
592
593 void CAEDRNativeAddDeviceSocketToList(JNIEnv *env, jobject deviceSocket)
594 {
595     OIC_LOG(DEBUG, TAG, "[EDR][Native] CANativeAddDeviceobjToList");
596
597     if (!deviceSocket)
598     {
599         OIC_LOG(ERROR, TAG, "[EDR][Native] Device is null");
600         return;
601     }
602
603     if (!g_deviceObjectList)
604     {
605         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
606         return;
607     }
608
609     jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
610     if (!jni_remoteAddress)
611     {
612         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_remoteAddress is null");
613         return;
614     }
615
616     const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
617
618     if (!CAEDRNativeIsDeviceSocketInList(env, remoteAddress))
619     {
620         jobject gDeviceSocker = (*env)->NewGlobalRef(env, deviceSocket);
621         u_arraylist_add(g_deviceObjectList, gDeviceSocker);
622         OIC_LOG(DEBUG, TAG, "Set Socket Object to Array");
623     }
624     (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, 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             CAEDRReorderingDeviceSocketList(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             CAEDRReorderingDeviceSocketList(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             return jarrayObj;
911         }
912         (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
913     }
914
915     return NULL;
916 }
917
918 uint32_t CAEDRGetSocketListLength()
919 {
920     if (!g_deviceObjectList)
921     {
922         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
923         return 0;
924     }
925
926     uint32_t length = u_arraylist_length(g_deviceObjectList);
927
928     return length;
929 }
930
931 void CAEDRReorderingDeviceSocketList(uint32_t index)
932 {
933     u_arraylist_remove(g_deviceObjectList, index);
934 }