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