Merge branch 'master' into simulator.
[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
342     (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
343     (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
344
345     return jni_isEnable;
346 }
347
348 jstring CAEDRNativeGetAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice)
349 {
350     if (!bluetoothDevice)
351     {
352         OIC_LOG(ERROR, TAG, "[EDR][Native] bluetoothDevice is null");
353         return NULL;
354     }
355     jclass jni_cid_device_list = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
356     if (!jni_cid_device_list)
357     {
358         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_cid_device_list is null");
359         return NULL;
360     }
361
362     jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_device_list, "getAddress",
363                                                        METHODID_STRINGNONPARAM);
364     if (!jni_mid_getAddress)
365     {
366         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_mid_getAddress is null");
367         return NULL;
368     }
369
370     jstring jni_address = (jstring)(*env)->CallObjectMethod(env, bluetoothDevice,
371                                                             jni_mid_getAddress);
372     if (!jni_address)
373     {
374         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_address is null");
375         return NULL;
376     }
377     return jni_address;
378 }
379
380 /**
381  * BT State List
382  */
383 void CAEDRNativeCreateDeviceStateList()
384 {
385     OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRNativeCreateDeviceStateList");
386
387     // create new object array
388     if (NULL == g_deviceStateList)
389     {
390         OIC_LOG(DEBUG, TAG, "Create device list");
391
392         g_deviceStateList = u_arraylist_create();
393     }
394 }
395
396 void CAEDRUpdateDeviceState(CAConnectedState_t state, const char *address)
397 {
398     if (!address)
399     {
400         OIC_LOG(ERROR, TAG, "[EDR][Native] address is null");
401         return;
402     }
403     state_t *newstate = (state_t*) OICCalloc(1, sizeof(state_t));
404     if (!newstate)
405     {
406         OIC_LOG(ERROR, TAG, "[EDR][Native] newstate is null");
407         return;
408     }
409     OICStrcpy((char*) newstate->address, sizeof(newstate->address), address);
410     newstate->state = state;
411
412     CAEDRNativeAddDeviceStateToList(newstate);
413 }
414
415 void CAEDRNativeAddDeviceStateToList(state_t *state)
416 {
417     if (!state)
418     {
419         OIC_LOG(ERROR, TAG, "[EDR][Native] device is null");
420         return;
421     }
422
423     if (!g_deviceStateList)
424     {
425         OIC_LOG(ERROR, TAG, "[EDR][Native] gdevice_list is null");
426         return;
427     }
428
429     if (CAEDRNativeIsDeviceInList((const char*) state->address))
430     {
431         // delete previous state for update new state
432         CAEDRNativeRemoveDevice((const char*) state->address);
433     }
434     u_arraylist_add(g_deviceStateList, state); // update new state
435     OIC_LOG_V(DEBUG, TAG, "Set State Info to List : %d", state->state);
436 }
437
438 bool CAEDRNativeIsDeviceInList(const char* remoteAddress)
439 {
440
441     if (!remoteAddress)
442     {
443         OIC_LOG(ERROR, TAG, "[EDR][Native] remoteAddress is null");
444         return false;
445     }
446     jint index;
447     jint length = u_arraylist_length(g_deviceStateList);
448     for (index = 0; index < length; index++)
449     {
450         state_t* state = (state_t*) u_arraylist_get(g_deviceStateList, index);
451         if (!state)
452         {
453             OIC_LOG(ERROR, TAG, "[EDR][Native] state_t object is null");
454             return false;
455         }
456
457         if (!strcmp(remoteAddress, (const char*) state->address))
458         {
459             OIC_LOG(DEBUG, TAG, "the device is already set");
460             return true;
461         }
462         else
463         {
464             continue;
465         }
466     }
467
468     OIC_LOG(DEBUG, TAG, "there are no the device in list.");
469     return false;
470 }
471
472 void CAEDRNativeRemoveAllDeviceState()
473 {
474     OIC_LOG(DEBUG, TAG, "CAEDRNativeRemoveAllDevices");
475
476     if (!g_deviceStateList)
477     {
478         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceStateList is null");
479         return;
480     }
481
482     jint index;
483     jint length = u_arraylist_length(g_deviceStateList);
484     for (index = 0; index < length; index++)
485     {
486         state_t* state = (state_t*) u_arraylist_get(g_deviceStateList, index);
487         if (!state)
488         {
489             OIC_LOG(DEBUG, TAG, "[EDR][Native] jarrayObj is null");
490             continue;
491         }
492         OICFree(state);
493     }
494
495     OICFree(g_deviceStateList);
496     g_deviceStateList = NULL;
497     return;
498 }
499
500 void CAEDRNativeRemoveDevice(const char *remoteAddress)
501 {
502     OIC_LOG(DEBUG, TAG, "CAEDRNativeRemoveDeviceforStateList");
503
504     if (!g_deviceStateList)
505     {
506         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceStateList is null");
507         return;
508     }
509     if (!remoteAddress)
510     {
511         OIC_LOG(ERROR, TAG, "[EDR][Native] remoteAddress is null");
512         return;
513     }
514
515     jint index;
516     jint length = u_arraylist_length(g_deviceStateList);
517     for (index = 0; index < length; index++)
518     {
519         state_t* state = (state_t*) u_arraylist_get(g_deviceStateList, index);
520         if (!state)
521         {
522             OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
523             continue;
524         }
525
526         if (!strcmp((const char*) state->address, remoteAddress))
527         {
528             OIC_LOG_V(DEBUG, TAG, "[EDR][Native] remove state : %s", remoteAddress);
529             OICFree(state);
530
531             u_arraylist_remove(g_deviceStateList, index);
532             break;
533         }
534     }
535     return;
536 }
537
538 CAConnectedState_t CAEDRIsConnectedDevice(const char *remoteAddress)
539 {
540     OIC_LOG(DEBUG, TAG, "CAEDRIsConnectedDevice");
541
542     if (!remoteAddress)
543     {
544         OIC_LOG(ERROR, TAG, "[EDR][Native] remoteAddress is null");
545         return STATE_DISCONNECTED;
546     }
547
548     if (!g_deviceStateList)
549     {
550         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceStateList is null");
551         return STATE_DISCONNECTED;
552     }
553
554     jint index;
555     jint length = u_arraylist_length(g_deviceStateList);
556     for (index = 0; index < length; index++)
557     {
558         state_t* state = (state_t*) u_arraylist_get(g_deviceStateList, index);
559         if (!state)
560         {
561             OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
562             continue;
563         }
564
565         if (!strcmp((const char*) state->address, remoteAddress))
566         {
567             return state->state;
568         }
569     }
570     return STATE_DISCONNECTED;
571 }
572
573 /**
574  * Device Socket Object List
575  */
576 void CAEDRNativeCreateDeviceSocketList()
577 {
578     OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRNativeCreateDeviceSocketList");
579
580     // create new object array
581     if (NULL == g_deviceObjectList)
582     {
583         OIC_LOG(DEBUG, TAG, "Create Device object list");
584
585         g_deviceObjectList = u_arraylist_create();
586     }
587 }
588
589 void CAEDRNativeAddDeviceSocketToList(JNIEnv *env, jobject deviceSocket)
590 {
591     OIC_LOG(DEBUG, TAG, "[EDR][Native] CANativeAddDeviceobjToList");
592
593     if (!deviceSocket)
594     {
595         OIC_LOG(ERROR, TAG, "[EDR][Native] Device is null");
596         return;
597     }
598
599     if (!g_deviceObjectList)
600     {
601         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
602         return;
603     }
604
605     jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
606     if (!jni_remoteAddress)
607     {
608         OIC_LOG(ERROR, TAG, "[EDR][Native] jni_remoteAddress is null");
609         return;
610     }
611
612     const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
613
614     if (!CAEDRNativeIsDeviceSocketInList(env, remoteAddress))
615     {
616         jobject gDeviceSocker = (*env)->NewGlobalRef(env, deviceSocket);
617         u_arraylist_add(g_deviceObjectList, gDeviceSocker);
618         OIC_LOG(DEBUG, TAG, "Set Socket Object to Array");
619     }
620     (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
621     (*env)->DeleteLocalRef(env, jni_remoteAddress);
622 }
623
624 bool CAEDRNativeIsDeviceSocketInList(JNIEnv *env, const char* remoteAddress)
625 {
626     OIC_LOG(DEBUG, TAG, "[EDR][Native] CANativeIsDeviceObjInList");
627
628     if (!remoteAddress)
629     {
630         OIC_LOG(ERROR, TAG, "[EDR][Native] remoteAddress is null");
631         return false;
632     }
633     jint index;
634     jint length = u_arraylist_length(g_deviceStateList);
635     for (index = 0; index < length; index++)
636     {
637
638         jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
639         if (!jarrayObj)
640         {
641             OIC_LOG(DEBUG, TAG, "[EDR][Native] jarrayObj is null");
642             return false;
643         }
644
645         jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
646         if (!jni_setAddress)
647         {
648             OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_setAddress is null");
649             return false;
650         }
651
652         const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
653         if (!setAddress)
654         {
655             OIC_LOG(DEBUG, TAG, "[EDR][Native] setAddress is null");
656             return false;
657         }
658
659         if (!strcmp(remoteAddress, setAddress))
660         {
661             OIC_LOG(DEBUG, TAG, "the device is already set");
662             (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
663             return true;
664         }
665         else
666         {
667             (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
668             continue;
669         }
670     }
671
672     OIC_LOG(DEBUG, TAG, "there are no the Device obejct in list. we can add");
673     return false;
674 }
675
676 void CAEDRNativeSocketCloseToAll(JNIEnv *env)
677 {
678     OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRNativeSocketCloseToAll");
679
680     if (!g_deviceObjectList)
681     {
682         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
683         return;
684     }
685
686     jclass jni_cid_BTSocket = (*env)->FindClass(env, CLASSPATH_BT_SOCKET);
687     if (!jni_cid_BTSocket)
688     {
689         OIC_LOG(ERROR, TAG, "[EDR][Native] close: jni_cid_BTSocket is null");
690         return;
691     }
692
693     jmethodID jni_mid_close = (*env)->GetMethodID(env, jni_cid_BTSocket, "close", "()V");
694     if (!jni_mid_close)
695     {
696         OIC_LOG(ERROR, TAG, "[EDR][Native] close: jni_mid_close is null");
697         return;
698     }
699
700     jint index;
701     jint length = u_arraylist_length(g_deviceStateList);
702     for (index = 0; index < length; index++)
703     {
704         jobject jni_obj_socket = (jobject) u_arraylist_get(g_deviceObjectList, index);
705         if (!jni_obj_socket)
706         {
707             OIC_LOG(ERROR, TAG, "[EDR][Native] socket obj is null");
708             return;
709         }
710
711         (*env)->CallVoidMethod(env, jni_obj_socket, jni_mid_close);
712
713         if ((*env)->ExceptionCheck(env))
714         {
715             OIC_LOG(ERROR, TAG, "[EDR][Native] close: close is Failed!!!");
716             (*env)->ExceptionDescribe(env);
717             (*env)->ExceptionClear(env);
718             return;
719         }
720     }
721 }
722
723 void CAEDRNativeRemoveAllDeviceSocket(JNIEnv *env)
724 {
725     OIC_LOG(DEBUG, TAG, "CANativeRemoveAllDeviceObjsList");
726
727     if (!g_deviceObjectList)
728     {
729         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
730         return;
731     }
732
733     jint index;
734     jint length = u_arraylist_length(g_deviceStateList);
735     for (index = 0; index < length; index++)
736     {
737         jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
738         if (!jarrayObj)
739         {
740             OIC_LOG(ERROR, TAG, "[EDR][Native] jarrayObj is null");
741             return;
742         }
743         (*env)->DeleteGlobalRef(env, jarrayObj);
744     }
745
746     OICFree(g_deviceObjectList);
747     g_deviceObjectList = NULL;
748     return;
749 }
750
751 void CAEDRNativeRemoveDeviceSocket(JNIEnv *env, jobject deviceSocket)
752 {
753     OIC_LOG(DEBUG, TAG, "CAEDRNativeRemoveDeviceSocket");
754
755     if (!g_deviceObjectList)
756     {
757         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
758         return;
759     }
760
761     jint index;
762     jint length = u_arraylist_length(g_deviceStateList);
763     for (index = 0; index < length; index++)
764     {
765         jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
766         if (!jarrayObj)
767         {
768             OIC_LOG(DEBUG, TAG, "[EDR][Native] jarrayObj is null");
769             continue;
770         }
771
772         jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
773         if (!jni_setAddress)
774         {
775             OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_setAddress is null");
776             continue;
777         }
778
779         jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
780         if (!jni_remoteAddress)
781         {
782             OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_remoteAddress is null");
783             continue;
784         }
785
786         const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
787         const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
788
789         if (!strcmp(setAddress, remoteAddress))
790         {
791             OIC_LOG_V(DEBUG, TAG, "[EDR][Native] remove object : %s", remoteAddress);
792             (*env)->DeleteGlobalRef(env, jarrayObj);
793             (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
794             (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
795
796             u_arraylist_remove(g_deviceObjectList, index);
797             break;
798         }
799         (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
800         (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
801     }
802
803     OIC_LOG(DEBUG, TAG, "[EDR][Native] there are no target object");
804     return;
805 }
806
807 void CAEDRNativeRemoveDeviceSocketBaseAddr(JNIEnv *env, jstring address)
808 {
809     OIC_LOG(DEBUG, TAG, "CAEDRNativeRemoveDeviceSocket");
810
811     if (!g_deviceObjectList)
812     {
813         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
814         return;
815     }
816
817     jint index;
818     jint length = u_arraylist_length(g_deviceStateList);
819     for (index = 0; index < length; index++)
820     {
821         jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
822         if (!jarrayObj)
823         {
824             OIC_LOG(DEBUG, TAG, "[EDR][Native] jarrayObj is null");
825             continue;
826         }
827
828         jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
829         if (!jni_setAddress)
830         {
831             OIC_LOG(ERROR, TAG, "[EDR][Native] jni_setAddress is null");
832             continue;
833         }
834         const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
835         const char* remoteAddress = (*env)->GetStringUTFChars(env, address, NULL);
836
837         if (!strcmp(setAddress, remoteAddress))
838         {
839             OIC_LOG_V(ERROR, TAG, "[EDR][Native] remove object : %s", remoteAddress);
840             (*env)->DeleteGlobalRef(env, jarrayObj);
841             (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
842             (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
843
844             u_arraylist_remove(g_deviceObjectList, index);
845             break;
846         }
847         (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
848         (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
849     }
850
851     OIC_LOG(DEBUG, TAG, "[EDR][Native] there are no target object");
852     return;
853 }
854
855 jobject CAEDRNativeGetDeviceSocket(uint32_t idx)
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             (*env)->DeleteLocalRef(env, jni_setAddress);
906             return jarrayObj;
907         }
908         (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
909         (*env)->DeleteLocalRef(env, jni_setAddress);
910     }
911
912     return NULL;
913 }
914
915 uint32_t CAEDRGetSocketListLength()
916 {
917     if (!g_deviceObjectList)
918     {
919         OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
920         return 0;
921     }
922
923     uint32_t length = u_arraylist_length(g_deviceObjectList);
924
925     return length;
926 }