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