Removed unnecessary EDR code related to u_arraylist_t
[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             u_arraylist_remove(g_deviceStateList, 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 /**
573  * Device Socket Object List
574  */
575 void CAEDRNativeCreateDeviceSocketList()
576 {
577     OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRNativeCreateDeviceSocketList");
578
579     // create new object array
580     if (NULL == g_deviceObjectList)
581     {
582         OIC_LOG(DEBUG, TAG, "Create Device object list");
583
584         g_deviceObjectList = u_arraylist_create();
585     }
586 }
587
588 void CAEDRNativeAddDeviceSocketToList(JNIEnv *env, jobject deviceSocket)
589 {
590     OIC_LOG(DEBUG, TAG, "[EDR][Native] CANativeAddDeviceobjToList");
591
592     if (!deviceSocket)
593     {
594         OIC_LOG(ERROR, TAG, "[EDR][Native] Device is null");
595         return;
596     }
597
598     if (!g_deviceObjectList)
599     {
600         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
601         return;
602     }
603
604     jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
605     if (!jni_remoteAddress)
606     {
607         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_remoteAddress is null");
608         return;
609     }
610
611     const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
612
613     if (!CAEDRNativeIsDeviceSocketInList(env, remoteAddress))
614     {
615         jobject gDeviceSocker = (*env)->NewGlobalRef(env, deviceSocket);
616         u_arraylist_add(g_deviceObjectList, gDeviceSocker);
617         OIC_LOG(DEBUG, TAG, "Set Socket Object to Array");
618     }
619     (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
620 }
621
622 bool CAEDRNativeIsDeviceSocketInList(JNIEnv *env, const char* remoteAddress)
623 {
624     OIC_LOG(DEBUG, TAG, "[EDR][Native] CANativeIsDeviceObjInList");
625
626     if (!remoteAddress)
627     {
628         OIC_LOG(ERROR, TAG, "[EDR][Native] remoteAddress is null");
629         return false;
630     }
631     jint index;
632     jint length = u_arraylist_length(g_deviceStateList);
633     for (index = 0; index < length; index++)
634     {
635
636         jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
637         if (!jarrayObj)
638         {
639             OIC_LOG(DEBUG, TAG, "[EDR][Native] jarrayObj is null");
640             return false;
641         }
642
643         jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
644         if (!jni_setAddress)
645         {
646             OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_setAddress is null");
647             return false;
648         }
649
650         const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
651         if (!setAddress)
652         {
653             OIC_LOG(DEBUG, TAG, "[EDR][Native] setAddress is null");
654             return false;
655         }
656
657         if (!strcmp(remoteAddress, setAddress))
658         {
659             OIC_LOG(DEBUG, TAG, "the device is already set");
660             (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
661             return true;
662         }
663         else
664         {
665             (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
666             continue;
667         }
668     }
669
670     OIC_LOG(DEBUG, TAG, "there are no the Device obejct in list. we can add");
671     return false;
672 }
673
674 void CAEDRNativeSocketCloseToAll(JNIEnv *env)
675 {
676     OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRNativeSocketCloseToAll");
677
678     if (!g_deviceObjectList)
679     {
680         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
681         return;
682     }
683
684     jclass jni_cid_BTSocket = (*env)->FindClass(env, CLASSPATH_BT_SOCKET);
685     if (!jni_cid_BTSocket)
686     {
687         OIC_LOG(ERROR, TAG, "[EDR][Native] close: jni_cid_BTSocket is null");
688         return;
689     }
690
691     jmethodID jni_mid_close = (*env)->GetMethodID(env, jni_cid_BTSocket, "close", "()V");
692     if (!jni_mid_close)
693     {
694         OIC_LOG(ERROR, TAG, "[EDR][Native] close: jni_mid_close is null");
695         return;
696     }
697
698     jint index;
699     jint length = u_arraylist_length(g_deviceStateList);
700     for (index = 0; index < length; index++)
701     {
702         jobject jni_obj_socket = (jobject) u_arraylist_get(g_deviceObjectList, index);
703         if (!jni_obj_socket)
704         {
705             OIC_LOG(ERROR, TAG, "[EDR][Native] socket obj is null");
706             return;
707         }
708
709         (*env)->CallVoidMethod(env, jni_obj_socket, jni_mid_close);
710
711         if ((*env)->ExceptionCheck(env))
712         {
713             OIC_LOG(ERROR, TAG, "[EDR][Native] close: close is Failed!!!");
714             (*env)->ExceptionDescribe(env);
715             (*env)->ExceptionClear(env);
716             return;
717         }
718     }
719 }
720
721 void CAEDRNativeRemoveAllDeviceSocket(JNIEnv *env)
722 {
723     OIC_LOG(DEBUG, TAG, "CANativeRemoveAllDeviceObjsList");
724
725     if (!g_deviceObjectList)
726     {
727         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
728         return;
729     }
730
731     jint index;
732     jint length = u_arraylist_length(g_deviceStateList);
733     for (index = 0; index < length; index++)
734     {
735         jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
736         if (!jarrayObj)
737         {
738             OIC_LOG(ERROR, TAG, "[EDR][Native] jarrayObj is null");
739             return;
740         }
741         (*env)->DeleteGlobalRef(env, jarrayObj);
742     }
743
744     OICFree(g_deviceObjectList);
745     g_deviceObjectList = NULL;
746     return;
747 }
748
749 void CAEDRNativeRemoveDeviceSocket(JNIEnv *env, jobject deviceSocket)
750 {
751     OIC_LOG(DEBUG, TAG, "CAEDRNativeRemoveDeviceSocket");
752
753     if (!g_deviceObjectList)
754     {
755         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
756         return;
757     }
758
759     jint index;
760     jint length = u_arraylist_length(g_deviceStateList);
761     for (index = 0; index < length; index++)
762     {
763         jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
764         if (!jarrayObj)
765         {
766             OIC_LOG(DEBUG, TAG, "[EDR][Native] jarrayObj is null");
767             continue;
768         }
769
770         jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
771         if (!jni_setAddress)
772         {
773             OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_setAddress is null");
774             continue;
775         }
776
777         jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
778         if (!jni_remoteAddress)
779         {
780             OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_remoteAddress is null");
781             continue;
782         }
783
784         const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
785         const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
786
787         if (!strcmp(setAddress, remoteAddress))
788         {
789             OIC_LOG_V(DEBUG, TAG, "[EDR][Native] remove object : %s", remoteAddress);
790             (*env)->DeleteGlobalRef(env, jarrayObj);
791             (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
792             (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
793
794             u_arraylist_remove(g_deviceObjectList, index);
795             break;
796         }
797         (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
798         (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
799     }
800
801     OIC_LOG(DEBUG, TAG, "[EDR][Native] there are no target object");
802     return;
803 }
804
805 void CAEDRNativeRemoveDeviceSocketBaseAddr(JNIEnv *env, jstring address)
806 {
807     OIC_LOG(DEBUG, TAG, "CAEDRNativeRemoveDeviceSocket");
808
809     if (!g_deviceObjectList)
810     {
811         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
812         return;
813     }
814
815     jint index;
816     jint length = u_arraylist_length(g_deviceStateList);
817     for (index = 0; index < length; index++)
818     {
819         jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
820         if (!jarrayObj)
821         {
822             OIC_LOG(DEBUG, TAG, "[EDR][Native] jarrayObj is null");
823             continue;
824         }
825
826         jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
827         if (!jni_setAddress)
828         {
829             OIC_LOG(ERROR, TAG, "[EDR][Native] jni_setAddress is null");
830             continue;
831         }
832         const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
833         const char* remoteAddress = (*env)->GetStringUTFChars(env, address, NULL);
834
835         if (!strcmp(setAddress, remoteAddress))
836         {
837             OIC_LOG_V(ERROR, TAG, "[EDR][Native] remove object : %s", remoteAddress);
838             (*env)->DeleteGlobalRef(env, jarrayObj);
839             (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
840             (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
841
842             u_arraylist_remove(g_deviceObjectList, index);
843             break;
844         }
845         (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
846         (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
847     }
848
849     OIC_LOG(DEBUG, TAG, "[EDR][Native] there are no target object");
850     return;
851 }
852
853 jobject CAEDRNativeGetDeviceSocket(uint32_t idx)
854 {
855     OIC_LOG(DEBUG, TAG, "CAEDRNativeGetDeviceSocket");
856
857     if (!g_deviceObjectList)
858     {
859         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
860         return NULL;
861     }
862
863     jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, idx);
864     if (!jarrayObj)
865     {
866         OIC_LOG(ERROR, TAG, "[EDR][Native] jarrayObj is not available");
867         return NULL;
868     }
869     return jarrayObj;
870 }
871
872 jobject CAEDRNativeGetDeviceSocketBaseAddr(JNIEnv *env, const char* remoteAddress)
873 {
874     OIC_LOG(DEBUG, TAG, "CAEDRNativeGetDeviceSocket");
875
876     if (!g_deviceObjectList)
877     {
878         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
879         return NULL;
880     }
881
882     jint index;
883     jint length = u_arraylist_length(g_deviceStateList);
884     for (index = 0; index < length; index++)
885     {
886         jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
887         if (!jarrayObj)
888         {
889             OIC_LOG(ERROR, TAG, "[EDR][Native] jarrayObj is null");
890             continue;
891         }
892
893         jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
894         if (!jni_setAddress)
895         {
896             OIC_LOG(ERROR, TAG, "[EDR][Native] jni_setAddress is null");
897             continue;
898         }
899         const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
900
901         if (!strcmp(setAddress, remoteAddress))
902         {
903             OIC_LOG_V(ERROR, TAG, "[EDR][Native] remove object : %s", remoteAddress);
904             (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
905             return jarrayObj;
906         }
907         (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
908     }
909
910     return NULL;
911 }
912
913 uint32_t CAEDRGetSocketListLength()
914 {
915     if (!g_deviceObjectList)
916     {
917         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
918         return 0;
919     }
920
921     uint32_t length = u_arraylist_length(g_deviceObjectList);
922
923     return length;
924 }