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