replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / connectivity / util / src / camanager / bt_le_manager / android / camanagerleutil.c
1 /* ****************************************************************
2  *
3  * Copyright 2016 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 "cacommon.h"
23 #include "logger.h"
24 #include "cacommonutil.h"
25 #include "camanagerleutil.h"
26 #include "caleclient.h"
27 #include "caleutils.h"
28
29 #define TAG "OIC_CA_MANAGER_UTIL"
30
31 static const char CLASSPATH_CONTENT_PREF[] = "android/content/SharedPreferences";
32 static const char CLASSPATH_CONTENT_PREF_EDITOR[] = "android/content/SharedPreferences$Editor";
33 static const char CLASSPATH_CONTEXT[] = "android/content/Context";
34
35 static const char PREF_NAME_CONNECTED_DEVICE[] = "PREF_NAME_CONNECTED_DEVICE";
36 static const char PREF_KEY_CONNECTED_DEVICE[] = "PREF_KEY_CONNECTED_DEVICE";
37
38 static const char METHODID_BOOLEAN_RET_STRING_PARAM[] = "(Ljava/lang/String;)Z";
39
40 static jobject CAManagerGetAdapterContext(JNIEnv *env)
41 {
42     OIC_LOG(DEBUG, TAG, "CAManagerGetAdapterContext");
43     VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
44
45     jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
46     if (!jni_cid_BTAdapter)
47     {
48         OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter: jni_cid_BTAdapter is null");
49         return NULL;
50     }
51
52     jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
53                                                                     "getDefaultAdapter",
54                                                                     METHODID_OBJECTNONPARAM);
55     if (!jni_mid_getDefaultAdapter)
56     {
57         OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
58         return NULL;
59     }
60
61     jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
62                                                                jni_mid_getDefaultAdapter);
63     if (!jni_obj_BTAdapter)
64     {
65         OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
66         return NULL;
67     }
68     return jni_obj_BTAdapter;
69 }
70
71 jobject CAManagerGetRemoteDevice(JNIEnv *env, jstring address)
72 {
73     OIC_LOG(DEBUG, TAG, "IN - CAManagerGetRemoteDevice");
74
75     VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
76     VERIFY_NON_NULL_RET(address, TAG, "address is null", NULL);
77
78     jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
79     if (!jni_cid_BTAdapter)
80     {
81         OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null");
82         return NULL;
83     }
84
85     // get remote bt adapter method
86     jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
87                                                                     "getDefaultAdapter",
88                                                                     METHODID_OBJECTNONPARAM);
89     if (!jni_mid_getDefaultAdapter)
90     {
91         OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
92         return NULL;
93     }
94
95     // gat bt adapter object
96     jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
97                                                                jni_mid_getDefaultAdapter);
98     if (!jni_obj_BTAdapter)
99     {
100         OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
101         return NULL;
102     }
103
104     jmethodID jni_mid_getRemoteDevice = (*env)->GetMethodID(env, jni_cid_BTAdapter,
105                                                             "getRemoteDevice",
106                                                             METHODID_BT_REMOTE_DEVICE);
107     if (!jni_mid_getRemoteDevice)
108     {
109         OIC_LOG(ERROR, TAG, "jni_mid_getRemoteDevice is null");
110         return NULL;
111     }
112
113     jobject jni_obj_device = (*env)->CallObjectMethod(env, jni_obj_BTAdapter,
114                                                       jni_mid_getRemoteDevice,
115                                                       address);
116     if (!jni_obj_device)
117     {
118         OIC_LOG(ERROR, TAG, "jni_obj_device is null");
119         return NULL;
120     }
121
122     OIC_LOG(DEBUG, TAG, "OUT - CAManagerGetRemoteDevice");
123     return jni_obj_device;
124 }
125
126 bool CAManagerIsDeviceBonded(JNIEnv *env, jobject btDevice)
127 {
128     OIC_LOG(DEBUG, TAG, "IN - CAManagerIsDeviceBonded");
129
130     VERIFY_NON_NULL(env, TAG, "env is null");
131     VERIFY_NON_NULL(btDevice, TAG, "btDevice is null");
132
133     jclass jni_cid_BTDevice = (*env)->FindClass(env, CLASSPATH_BT_DEVICE);
134     if (!jni_cid_BTDevice)
135     {
136         OIC_LOG(ERROR, TAG, "jni_cid_BTDevice is null");
137         return false;
138     }
139
140     jmethodID jni_mid_getBondState = (*env)->GetMethodID(env, jni_cid_BTDevice,
141                                                          "getBondState", "()I");
142     if (!jni_mid_getBondState)
143     {
144         OIC_LOG(ERROR, TAG, "jni_mid_getBondState is null");
145         return false;
146     }
147
148     jint jni_bond_state = (*env)->CallIntMethod(env, btDevice, jni_mid_getBondState);
149     OIC_LOG_V(DEBUG, TAG, "bond state is %d", jni_bond_state);
150
151     // BOND_BONDED - Constant value : 12 (0x0000000c)
152     jfieldID id_bonded = (*env)->GetStaticFieldID(env, jni_cid_BTDevice,
153                                                   "BOND_BONDED", "I");
154     if (!id_bonded)
155     {
156         OIC_LOG(ERROR, TAG, "id_bonded is null");
157         return false;;
158     }
159
160     jint jni_bonded_const = (*env)->GetStaticIntField(env, jni_cid_BTDevice, id_bonded);
161
162     if (jni_bond_state == jni_bonded_const)
163     {
164         OIC_LOG(INFO, TAG, "it is not bonded to each other");
165         return false;
166     }
167     OIC_LOG(DEBUG, TAG, "OUT - CAManagerIsDeviceBonded");
168     return true;
169 }
170
171 bool CAManagerControlAdapter(JNIEnv *env, bool control_flag)
172 {
173     OIC_LOG(DEBUG, TAG, "IN - CAManagerControlAdapter");
174
175     VERIFY_NON_NULL_RET(env, TAG, "env", false);
176
177     jobject jni_obj_BTAdapter = CAManagerGetAdapterContext(env);
178     if (!jni_obj_BTAdapter)
179     {
180         OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
181         return false;
182     }
183
184     jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
185     if (!jni_cid_BTAdapter)
186     {
187         OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter: jni_cid_BTAdapter is null");
188         return NULL;
189     }
190
191     OIC_LOG_V(DEBUG, TAG, "change adapter : %d", control_flag);
192     jmethodID jni_mid_control = NULL;
193     if (control_flag)
194     {
195         // enable()
196         jni_mid_control = (*env)->GetMethodID(env, jni_cid_BTAdapter, "enable", "()Z");
197     }
198     else
199     {
200         // disable()
201         jni_mid_control = (*env)->GetMethodID(env, jni_cid_BTAdapter, "disable", "()Z");
202     }
203
204     if (!jni_mid_control)
205     {
206         OIC_LOG(ERROR, TAG, "jni_mid_control is null");
207         return false;
208     }
209
210     OIC_LOG(DEBUG, TAG, "CALL API - Adapter Will be Changed");
211     jboolean jni_res = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_control);
212
213     return jni_res;
214 }
215
216 CAResult_t CAManagerReadRemoteRssi(JNIEnv *env, jobject bluetoothGatt)
217 {
218     VERIFY_NON_NULL(env, TAG, "env is null");
219     VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
220
221     if (!CALEIsEnableBTAdapter(env))
222     {
223         OIC_LOG(INFO, TAG, "BT adapter is not enabled");
224         return CA_ADAPTER_NOT_ENABLED;
225     }
226
227     // get BluetoothGatt class
228     jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
229     if (!jni_cid_BluetoothGatt)
230     {
231         OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
232         return CA_STATUS_FAILED;
233     }
234
235     jmethodID jni_mid_readRemoteRssi = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
236                                                              "readRemoteRssi", "()Z");
237     if (!jni_mid_readRemoteRssi)
238     {
239         OIC_LOG(ERROR, TAG, "jni_mid_readRemoteRssi is null");
240         return CA_STATUS_FAILED;
241     }
242     // call disconnect gatt method
243     jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_readRemoteRssi);
244     if (!ret)
245     {
246         OIC_LOG(ERROR, TAG, "readremoteRssi has not been called");
247         return CA_STATUS_FAILED;
248     }
249
250     return CA_STATUS_OK;
251 }
252
253 jobject CAManagerGetSharedPreference(JNIEnv *env, jobject context, jstring prefName)
254 {
255     VERIFY_NON_NULL_RET(env, TAG, "env", NULL);
256     VERIFY_NON_NULL_RET(context, TAG, "context", NULL);
257     VERIFY_NON_NULL_RET(prefName, TAG, "prefName", NULL);
258
259     jclass jni_cls_context = (*env)->FindClass(env, CLASSPATH_CONTEXT);
260     if (!jni_cls_context)
261     {
262         OIC_LOG(ERROR, TAG, "jni_cls_context is null");
263         return NULL;
264     }
265
266     // getSharedPreferences
267     jmethodID jni_mid_getPref = (*env)->GetMethodID(env, jni_cls_context, "getSharedPreferences",
268                                                     "(Ljava/lang/String;I)"
269                                                     "Landroid/content/SharedPreferences;");
270     if (!jni_mid_getPref)
271     {
272         OIC_LOG(ERROR, TAG, "jni_mid_getPref is null");
273         return NULL;
274     }
275
276     jobject jni_obj_sharedPref = (*env)->CallObjectMethod(env, context,
277                                                           jni_mid_getPref,
278                                                           prefName,
279                                                           0);
280     if (!jni_obj_sharedPref)
281     {
282         OIC_LOG(ERROR, TAG, "jni_obj_sharedPref is null");
283         return NULL;
284     }
285
286     return jni_obj_sharedPref;
287 }
288
289 jobject CAManagerGetPrefEditObject(JNIEnv *env, jobject context)
290 {
291     OIC_LOG(DEBUG, TAG, "IN-CAManagerGetPrefEditObject");
292     VERIFY_NON_NULL_RET(env, TAG, "env", NULL);
293     VERIFY_NON_NULL_RET(context, TAG, "context", NULL);
294
295     jstring jni_str_prefName = (*env)->NewStringUTF(env, PREF_NAME_CONNECTED_DEVICE);
296     if (!jni_str_prefName)
297     {
298         OIC_LOG(ERROR, TAG, "jni_str_prefName is null");
299         return NULL;
300     }
301
302     jstring jni_str_prefKey = (*env)->NewStringUTF(env, PREF_KEY_CONNECTED_DEVICE);
303     if (!jni_str_prefKey)
304     {
305         OIC_LOG(ERROR, TAG, "jni_str_prefKey is null");
306         return NULL;
307     }
308
309     // get SharedPreference
310     jobject jni_obj_pref = CAManagerGetSharedPreference(env, context, jni_str_prefName);
311     if (!jni_obj_pref)
312     {
313         OIC_LOG(ERROR, TAG, "jni_obj_pref is null");
314         return NULL;
315     }
316
317     // edit()
318     jobject jni_cls_pref = (*env)->FindClass(env, CLASSPATH_CONTENT_PREF);
319     if (!jni_cls_pref)
320     {
321         OIC_LOG(ERROR, TAG, "jni_cls_pref is null");
322         return NULL;
323     }
324
325     jmethodID jni_mid_prefEdit = (*env)->GetMethodID(env, jni_cls_pref, "edit",
326                                                      "()Landroid/content/SharedPreferences$Editor;");
327     if (!jni_mid_prefEdit)
328     {
329         OIC_LOG(ERROR, TAG, "jni_mid_prefEdit is null");
330         return NULL;
331     }
332
333     jobject jni_obj_prefEdit = (*env)->CallObjectMethod(env, jni_obj_pref, jni_mid_prefEdit);
334     if (!jni_obj_prefEdit)
335     {
336         OIC_LOG(ERROR, TAG, "jni_obj_prefEdit is null");
337         return NULL;
338     }
339
340     OIC_LOG(DEBUG, TAG, "OUT-CAManagerGetPrefEditObject");
341     return jni_obj_prefEdit;
342 }
343
344 bool CAManagerUpdatePrefStringSet(JNIEnv *env, jobject context, jobject set)
345 {
346     OIC_LOG(DEBUG, TAG, "IN-CAManagerUpdatePrefStringSet");
347
348     VERIFY_NON_NULL_RET(env, TAG, "env", false);
349     VERIFY_NON_NULL_RET(context, TAG, "context", false);
350     VERIFY_NON_NULL_RET(set, TAG, "set", false);
351
352     jstring jni_str_prefKey = (*env)->NewStringUTF(env, PREF_KEY_CONNECTED_DEVICE);
353     if (!jni_str_prefKey)
354     {
355         OIC_LOG(ERROR, TAG, "jni_str_prefKey is null");
356         return false;
357     }
358
359     jobject jni_obj_prefEdit = CAManagerGetPrefEditObject(env, context);
360     if (!jni_obj_prefEdit)
361     {
362         OIC_LOG(ERROR, TAG, "jni_obj_prefEdit is null");
363         return false;
364     }
365
366     // putString()
367     jobject jni_cls_edit = (*env)->FindClass(env, CLASSPATH_CONTENT_PREF_EDITOR);
368     if (!jni_cls_edit)
369     {
370         OIC_LOG(ERROR, TAG, "jni_cls_edit is null");
371         return false;
372     }
373
374     // get putString method interface
375     jmethodID jni_mid_PrefPutStringSet = (*env)->GetMethodID(env, jni_cls_edit, "putStringSet",
376                                                              "(Ljava/lang/String;Ljava/util/Set;)"
377                                                              "Landroid/content/SharedPreferences"
378                                                              "$Editor;");
379     if (!jni_mid_PrefPutStringSet)
380     {
381         OIC_LOG(ERROR, TAG, "jni_mid_PrefPutStringSet is null");
382         return false;
383     }
384
385     jobject gSetString = (jobject)(*env)->NewGlobalRef(env, set);
386     if (!gSetString)
387     {
388         OIC_LOG(ERROR, TAG, "gAddress is null");
389         return false;
390     }
391
392     OIC_LOG(DEBUG, TAG, "CALL API - request putString for SharedPreferences.Editor");
393     (*env)->CallObjectMethod(env, jni_obj_prefEdit, jni_mid_PrefPutStringSet,
394                              jni_str_prefKey, gSetString);
395
396     // get commit method interface
397     jmethodID jni_mid_PrefCommit = (*env)->GetMethodID(env, jni_cls_edit, "commit",
398                                                        "()Z");
399     if (!jni_mid_PrefCommit)
400     {
401         OIC_LOG(ERROR, TAG, "jni_mid_PrefCommit is null");
402         return false;
403     }
404
405     jboolean res = (*env)->CallBooleanMethod(env, jni_obj_prefEdit, jni_mid_PrefCommit);
406
407     OIC_LOG(DEBUG, TAG, "OUT-CAManagerUpdatePrefStringSet");
408     return res;
409 }
410
411 jobject CAManagerGetPrefStringSet(JNIEnv *env, jobject context)
412 {
413     OIC_LOG(DEBUG, TAG, "CAManagerGetPrefStringSet");
414
415     VERIFY_NON_NULL_RET(env, TAG, "env", NULL);
416     VERIFY_NON_NULL_RET(context, TAG, "context", NULL);
417
418     jstring jni_str_prefName = (*env)->NewStringUTF(env, PREF_NAME_CONNECTED_DEVICE);
419     if (!jni_str_prefName)
420     {
421         OIC_LOG(ERROR, TAG, "jni_str_prefName is null");
422         return NULL;
423     }
424
425     jstring jni_str_prefKey = (*env)->NewStringUTF(env, PREF_KEY_CONNECTED_DEVICE);
426     if (!jni_str_prefKey)
427     {
428         OIC_LOG(ERROR, TAG, "jni_str_prefKey is null");
429         return NULL;
430     }
431
432     // get SharedPreference
433     jobject jni_obj_pref = CAManagerGetSharedPreference(env, context, jni_str_prefName);
434     if (!jni_obj_pref)
435     {
436         OIC_LOG(ERROR, TAG, "jni_obj_pref is null");
437         return NULL;
438     }
439
440     // contains(String key)
441     jobject jni_cls_pref = (*env)->FindClass(env, CLASSPATH_CONTENT_PREF);
442     if (!jni_cls_pref)
443     {
444         OIC_LOG(ERROR, TAG, "jni_cls_pref is null");
445         return NULL;
446     }
447
448     jmethodID jni_mid_getStringSet = (*env)->GetMethodID(env, jni_cls_pref, "getStringSet",
449                                                          "(Ljava/lang/String;Ljava/util/Set;)"
450                                                          "Ljava/util/Set;");
451     if (!jni_mid_getStringSet)
452     {
453         OIC_LOG(ERROR, TAG, "jni_mid_getStringSet is null");
454         return NULL;
455     }
456
457     jobject jni_defSet = CAManagerCreateSetString(env);
458     if (!jni_defSet)
459     {
460         OIC_LOG(ERROR, TAG, "jni_defSet is null");
461         return NULL;
462     }
463
464     jobject value = (*env)->CallObjectMethod(env, jni_obj_pref,
465                                              jni_mid_getStringSet,
466                                              jni_str_prefKey,
467                                              jni_defSet);
468
469     return value;
470 }
471
472 bool CAManagerContainsPrefStringSet(JNIEnv *env, jobject context)
473 {
474     OIC_LOG(DEBUG, TAG, "CAManagerContainsPrefStringSet");
475
476     VERIFY_NON_NULL_RET(env, TAG, "env", false);
477     VERIFY_NON_NULL_RET(context, TAG, "context", false);
478
479     jstring jni_str_prefName = (*env)->NewStringUTF(env, PREF_NAME_CONNECTED_DEVICE);
480     if (!jni_str_prefName)
481     {
482         OIC_LOG(ERROR, TAG, "jni_str_prefName is null");
483         return false;
484     }
485
486     jstring jni_str_prefKey = (*env)->NewStringUTF(env, PREF_KEY_CONNECTED_DEVICE);
487     if (!jni_str_prefKey)
488     {
489         OIC_LOG(ERROR, TAG, "jni_str_prefKey is null");
490         return false;
491     }
492
493     // get SharedPreference
494     jobject jni_obj_pref = CAManagerGetSharedPreference(env, context, jni_str_prefName);
495     if (!jni_obj_pref)
496     {
497         OIC_LOG(ERROR, TAG, "jni_obj_pref is null");
498         return false;
499     }
500
501     // contains(String key)
502     jobject jni_cls_pref = (*env)->FindClass(env, CLASSPATH_CONTENT_PREF);
503     if (!jni_cls_pref)
504     {
505         OIC_LOG(ERROR, TAG, "jni_cls_pref is null");
506         return false;
507     }
508
509     jmethodID jni_mid_prefContains = (*env)->GetMethodID(env, jni_cls_pref, "contains",
510                                                          METHODID_BOOLEAN_RET_STRING_PARAM);
511     if (!jni_mid_prefContains)
512     {
513         OIC_LOG(ERROR, TAG, "jni_mid_prefContains is null");
514         return false;
515     }
516
517     jboolean res = (*env)->CallBooleanMethod(env, jni_obj_pref,
518                                              jni_mid_prefContains,
519                                              jni_str_prefKey);
520
521     OIC_LOG_V(DEBUG, TAG, "Preference - contains (%d)", res);
522     return res;
523 }
524
525 bool CAManagerRemovePrefStringSet(JNIEnv *env, jobject context)
526 {
527     OIC_LOG(DEBUG, TAG, "CAManagerRemovePrefStringSet");
528
529     VERIFY_NON_NULL_RET(env, TAG, "env", false);
530     VERIFY_NON_NULL_RET(context, TAG, "context", false);
531
532     jstring jni_str_prefKey = (*env)->NewStringUTF(env, PREF_KEY_CONNECTED_DEVICE);
533     if (!jni_str_prefKey)
534     {
535         OIC_LOG(ERROR, TAG, "jni_str_prefKey is null");
536         return false;
537     }
538
539     jobject jni_obj_prefEdit = CAManagerGetPrefEditObject(env, context);
540     if (!jni_obj_prefEdit)
541     {
542         OIC_LOG(ERROR, TAG, "jni_obj_prefEdit is null");
543         return false;
544     }
545
546     // putString()
547     jobject jni_cls_edit = (*env)->FindClass(env, CLASSPATH_CONTENT_PREF_EDITOR);
548     if (!jni_cls_edit)
549     {
550         OIC_LOG(ERROR, TAG, "jni_cls_edit is null");
551         return false;
552     }
553
554     // get remove method interface
555     jmethodID jni_mid_PrefRemove = (*env)->GetMethodID(env, jni_cls_edit, "remove",
556                                                        "(Ljava/lang/String;)"
557                                                        "Landroid/content/SharedPreferences"
558                                                        "$Editor;");
559     if (!jni_mid_PrefRemove)
560     {
561         OIC_LOG(ERROR, TAG, "jni_mid_PrefRemove is null");
562         return false;
563     }
564
565     OIC_LOG(DEBUG, TAG, "CALL API - request remove for SharedPreferences.Editor");
566     (*env)->CallObjectMethod(env, jni_obj_prefEdit, jni_mid_PrefRemove, jni_str_prefKey);
567
568     // get commit method interface
569     jmethodID jni_mid_PrefCommit = (*env)->GetMethodID(env, jni_cls_edit, "commit",
570                                                        "()Z");
571     if (!jni_mid_PrefCommit)
572     {
573         OIC_LOG(ERROR, TAG, "jni_mid_PrefCommit is null");
574         return false;
575     }
576
577     jboolean res = (*env)->CallBooleanMethod(env, jni_obj_prefEdit, jni_mid_PrefCommit);
578
579     OIC_LOG(DEBUG, TAG, "OUT-CAManagerAddConnectedDeviceAddress");
580     return res;
581 }
582
583 bool CAManagerAddConnectedDeviceAddress(JNIEnv *env, jobject context,
584                                         jstring address, jobject set)
585 {
586     OIC_LOG(DEBUG, TAG, "IN-CAManagerAddConnectedDeviceAddress");
587
588     VERIFY_NON_NULL_RET(env, TAG, "env", false);
589     VERIFY_NON_NULL_RET(context, TAG, "context", false);
590     VERIFY_NON_NULL_RET(address, TAG, "address", false);
591     VERIFY_NON_NULL_RET(set, TAG, "set", false);
592
593     if (CAManagerCallFuncSetString(env, address, set, CM_CONTAINS))
594     {
595         OIC_LOG(DEBUG, TAG, "it's already done");
596         return true;
597     }
598
599     if (!CAManagerCallFuncSetString(env, address, set, CM_ADD))
600     {
601         OIC_LOG(ERROR, TAG, "CAManagerAddSetString has failed");
602         return false;
603     }
604
605     return CAManagerUpdatePrefStringSet(env, context, set);
606 }
607
608 bool CAManagerIsConnectedDeviceAddress(JNIEnv *env, jobject context,
609                                        jstring address, jobject set)
610 {
611     VERIFY_NON_NULL_RET(env, TAG, "env", false);
612     VERIFY_NON_NULL_RET(context, TAG, "context", false);
613
614     return CAManagerCallFuncSetString(env, address, set, CM_CONTAINS);
615 }
616
617 jobject CAManagerGetConnectedDeviceAddress(JNIEnv *env, jobject context)
618 {
619     OIC_LOG(DEBUG, TAG, "CAManagerGetConnectedDeviceAddress");
620
621     if (!CAManagerContainsPrefStringSet(env, context))
622     {
623         OIC_LOG(DEBUG, TAG, "there is no set data");
624         return NULL;
625     }
626
627     return CAManagerGetPrefStringSet(env, context);
628 }
629
630 bool CAManagerRemoveConnectedDeviceAddress(JNIEnv *env, jobject context,
631                                            jstring address, jobject set)
632 {
633     OIC_LOG(DEBUG, TAG, "CAManagerRemoveConnectedDeviceAddress");
634
635     VERIFY_NON_NULL_RET(env, TAG, "env", false);
636     VERIFY_NON_NULL_RET(context, TAG, "context", false);
637     VERIFY_NON_NULL_RET(address, TAG, "address", false);
638     VERIFY_NON_NULL_RET(set, TAG, "set", false);
639
640     if (!CAManagerCallFuncSetString(env, address, set, CM_CONTAINS))
641     {
642         OIC_LOG(DEBUG, TAG, "it's already done");
643         return true;
644     }
645
646     if (!CAManagerCallFuncSetString(env, address, set, CM_REMOVE))
647     {
648         OIC_LOG(ERROR, TAG, "CAManagerAddSetString has failed");
649         return false;
650     }
651
652     return CAManagerUpdatePrefStringSet(env, context, set);
653 }
654
655 jboolean CAManagerCheckBTAddress(JNIEnv *env, jstring address)
656 {
657     OIC_LOG(DEBUG, TAG, "CAManagerCheckBTAddress");
658
659     VERIFY_NON_NULL_RET(env, TAG, "env is null", JNI_FALSE);
660     VERIFY_NON_NULL_RET(address, TAG, "address is null", JNI_FALSE);
661
662     jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
663     if (!jni_cid_BTAdapter)
664     {
665         OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null");
666         return JNI_FALSE;
667     }
668
669     // get remote bt adapter method
670     jmethodID jni_mid_checkBTAddress =
671             (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
672                                       "checkBluetoothAddress",
673                                       METHODID_BOOLEAN_RET_STRING_PARAM);
674     if (!jni_mid_checkBTAddress)
675     {
676         OIC_LOG(ERROR, TAG, "jni_mid_checkBTAddress is null");
677         return JNI_FALSE;
678     }
679
680     jboolean jni_obj_isAddress = (*env)->CallStaticBooleanMethod(env, jni_cid_BTAdapter,
681                                                                  jni_mid_checkBTAddress,
682                                                                  address);
683     return jni_obj_isAddress;
684 }
685
686 jobject CAManagerCreateSetString(JNIEnv *env)
687 {
688     OIC_LOG(DEBUG, TAG, "CAManagerCallFuncSetString");
689
690     VERIFY_NON_NULL_RET(env, TAG, "env", NULL);
691
692     jclass jclazzMap = (*env)->FindClass(env, "java/util/HashSet" );
693     if (!jclazzMap)
694     {
695         OIC_LOG(ERROR, TAG, "jclazzMap is null");
696         return NULL;
697     }
698
699     jmethodID jinitMap = (*env)->GetMethodID(env, jclazzMap, "<init>", "()V");
700     if (!jinitMap)
701     {
702         OIC_LOG(ERROR, TAG, "jinitMap is null");
703         return NULL;
704     }
705
706     jobject jpropertyMap = (*env)->NewObject(env, jclazzMap, jinitMap);
707     if (!jpropertyMap)
708     {
709         OIC_LOG(ERROR, TAG, "jpropertyMap is null");
710         return NULL;
711     }
712
713     return jpropertyMap;
714 }
715
716 bool CAManagerCallFuncSetString(JNIEnv *env, jstring address, jobject set,
717                                 CASetMethod_t method_type)
718 {
719     VERIFY_NON_NULL_RET(env, TAG, "env", false);
720     VERIFY_NON_NULL_RET(address, TAG, "address", false);
721     VERIFY_NON_NULL_RET(set, TAG, "set", false);
722
723     jclass jni_cls_set = (*env)->FindClass(env, "java/util/HashSet");
724     if (!jni_cls_set)
725     {
726         OIC_LOG(ERROR, TAG, "jni_cls_set is null");
727         return false;
728     }
729
730     jmethodID jni_mid_setMethod = NULL;
731     switch (method_type)
732     {
733         case CM_CONTAINS:
734             jni_mid_setMethod = (*env)->GetMethodID(env, jni_cls_set, "contains",
735                                                     "(Ljava/lang/Object;)Z");
736             break;
737         case CM_ADD:
738             jni_mid_setMethod = (*env)->GetMethodID(env, jni_cls_set, "add",
739                                                     "(Ljava/lang/Object;)Z");
740             break;
741         case CM_REMOVE:
742             jni_mid_setMethod = (*env)->GetMethodID(env, jni_cls_set, "remove",
743                                                     "(Ljava/lang/Object;)Z");
744             break;
745         default:
746             break;
747     }
748     if (!jni_mid_setMethod)
749     {
750         OIC_LOG(ERROR, TAG, "jni_mid_setMethod is null");
751         return false;
752     }
753
754     jboolean res = (*env)->CallBooleanMethod(env, set, jni_mid_setMethod, address);
755     return res;
756 }