Remove unused pkg dependancy
[platform/upstream/iotivity.git] / resource / csdk / connectivity / util / src / camanager / 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     OIC_LOG(DEBUG, TAG, "CAManagerReadRemoteRssi");
219
220     VERIFY_NON_NULL(env, TAG, "env is null");
221     VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
222
223     if (!CALEIsEnableBTAdapter(env))
224     {
225         OIC_LOG(INFO, TAG, "BT adapter is not enabled");
226         return CA_ADAPTER_NOT_ENABLED;
227     }
228
229     // get BluetoothGatt class
230     OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
231     jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
232     if (!jni_cid_BluetoothGatt)
233     {
234         OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
235         return CA_STATUS_FAILED;
236     }
237
238     OIC_LOG(DEBUG, TAG, "discovery gatt services method");
239     jmethodID jni_mid_readRemoteRssi = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
240                                                              "readRemoteRssi", "()Z");
241     if (!jni_mid_readRemoteRssi)
242     {
243         OIC_LOG(ERROR, TAG, "jni_mid_readRemoteRssi is null");
244         return CA_STATUS_FAILED;
245     }
246     // call disconnect gatt method
247     OIC_LOG(DEBUG, TAG, "CALL API - request readremoteRssi");
248     jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_readRemoteRssi);
249     if (!ret)
250     {
251         OIC_LOG(ERROR, TAG, "readremoteRssi has not been called");
252         return CA_STATUS_FAILED;
253     }
254
255     return CA_STATUS_OK;
256 }
257
258 jobject CAManagerGetSharedPreference(JNIEnv *env, jobject context, jstring prefName)
259 {
260     VERIFY_NON_NULL_RET(env, TAG, "env", NULL);
261     VERIFY_NON_NULL_RET(context, TAG, "context", NULL);
262     VERIFY_NON_NULL_RET(prefName, TAG, "prefName", NULL);
263
264     jclass jni_cls_context = (*env)->FindClass(env, CLASSPATH_CONTEXT);
265     if (!jni_cls_context)
266     {
267         OIC_LOG(ERROR, TAG, "jni_cls_context is null");
268         return NULL;
269     }
270
271     // getSharedPreferences
272     jmethodID jni_mid_getPref = (*env)->GetMethodID(env, jni_cls_context, "getSharedPreferences",
273                                                     "(Ljava/lang/String;I)"
274                                                     "Landroid/content/SharedPreferences;");
275     if (!jni_mid_getPref)
276     {
277         OIC_LOG(ERROR, TAG, "jni_mid_getPref is null");
278         return NULL;
279     }
280
281     jobject jni_obj_sharedPref = (*env)->CallObjectMethod(env, context,
282                                                           jni_mid_getPref,
283                                                           prefName,
284                                                           0);
285     if (!jni_obj_sharedPref)
286     {
287         OIC_LOG(ERROR, TAG, "jni_obj_sharedPref is null");
288         return NULL;
289     }
290
291     return jni_obj_sharedPref;
292 }
293
294 jobject CAManagerGetPrefEditObject(JNIEnv *env, jobject context)
295 {
296     OIC_LOG(DEBUG, TAG, "IN-CAManagerGetPrefEditObject");
297     VERIFY_NON_NULL_RET(env, TAG, "env", NULL);
298     VERIFY_NON_NULL_RET(context, TAG, "context", NULL);
299
300     jstring jni_str_prefName = (*env)->NewStringUTF(env, PREF_NAME_CONNECTED_DEVICE);
301     if (!jni_str_prefName)
302     {
303         OIC_LOG(ERROR, TAG, "jni_str_prefName is null");
304         return NULL;
305     }
306
307     jstring jni_str_prefKey = (*env)->NewStringUTF(env, PREF_KEY_CONNECTED_DEVICE);
308     if (!jni_str_prefKey)
309     {
310         OIC_LOG(ERROR, TAG, "jni_str_prefKey is null");
311         return NULL;
312     }
313
314     // get SharedPreference
315     jobject jni_obj_pref = CAManagerGetSharedPreference(env, context, jni_str_prefName);
316     if (!jni_obj_pref)
317     {
318         OIC_LOG(ERROR, TAG, "jni_obj_pref is null");
319         return NULL;
320     }
321
322     // edit()
323     jobject jni_cls_pref = (*env)->FindClass(env, CLASSPATH_CONTENT_PREF);
324     if (!jni_cls_pref)
325     {
326         OIC_LOG(ERROR, TAG, "jni_cls_pref is null");
327         return NULL;
328     }
329
330     jmethodID jni_mid_prefEdit = (*env)->GetMethodID(env, jni_cls_pref, "edit",
331                                                      "()Landroid/content/SharedPreferences$Editor;");
332     if (!jni_mid_prefEdit)
333     {
334         OIC_LOG(ERROR, TAG, "jni_mid_prefEdit is null");
335         return NULL;
336     }
337
338     jobject jni_obj_prefEdit = (*env)->CallObjectMethod(env, jni_obj_pref, jni_mid_prefEdit);
339     if (!jni_obj_prefEdit)
340     {
341         OIC_LOG(ERROR, TAG, "jni_obj_prefEdit is null");
342         return NULL;
343     }
344
345     OIC_LOG(DEBUG, TAG, "OUT-CAManagerGetPrefEditObject");
346     return jni_obj_prefEdit;
347 }
348
349 bool CAManagerUpdatePrefStringSet(JNIEnv *env, jobject context, jobject set)
350 {
351     OIC_LOG(DEBUG, TAG, "IN-CAManagerUpdatePrefStringSet");
352
353     VERIFY_NON_NULL_RET(env, TAG, "env", false);
354     VERIFY_NON_NULL_RET(context, TAG, "context", false);
355     VERIFY_NON_NULL_RET(set, TAG, "set", false);
356
357     jstring jni_str_prefKey = (*env)->NewStringUTF(env, PREF_KEY_CONNECTED_DEVICE);
358     if (!jni_str_prefKey)
359     {
360         OIC_LOG(ERROR, TAG, "jni_str_prefKey is null");
361         return false;
362     }
363
364     jobject jni_obj_prefEdit = CAManagerGetPrefEditObject(env, context);
365     if (!jni_obj_prefEdit)
366     {
367         OIC_LOG(ERROR, TAG, "jni_obj_prefEdit is null");
368         return false;
369     }
370
371     // putString()
372     jobject jni_cls_edit = (*env)->FindClass(env, CLASSPATH_CONTENT_PREF_EDITOR);
373     if (!jni_cls_edit)
374     {
375         OIC_LOG(ERROR, TAG, "jni_cls_edit is null");
376         return false;
377     }
378
379     // get putString method interface
380     jmethodID jni_mid_PrefPutStringSet = (*env)->GetMethodID(env, jni_cls_edit, "putStringSet",
381                                                              "(Ljava/lang/String;Ljava/util/Set;)"
382                                                              "Landroid/content/SharedPreferences"
383                                                              "$Editor;");
384     if (!jni_mid_PrefPutStringSet)
385     {
386         OIC_LOG(ERROR, TAG, "jni_mid_PrefPutStringSet is null");
387         return false;
388     }
389
390     jobject gSetString = (jobject)(*env)->NewGlobalRef(env, set);
391     if (!gSetString)
392     {
393         OIC_LOG(ERROR, TAG, "gAddress is null");
394         return false;
395     }
396
397     OIC_LOG(DEBUG, TAG, "CALL API - request putString for SharedPreferences.Editor");
398     (*env)->CallObjectMethod(env, jni_obj_prefEdit, jni_mid_PrefPutStringSet,
399                              jni_str_prefKey, gSetString);
400
401     // get commit method interface
402     jmethodID jni_mid_PrefCommit = (*env)->GetMethodID(env, jni_cls_edit, "commit",
403                                                        "()Z");
404     if (!jni_mid_PrefCommit)
405     {
406         OIC_LOG(ERROR, TAG, "jni_mid_PrefCommit is null");
407         return false;
408     }
409
410     jboolean res = (*env)->CallBooleanMethod(env, jni_obj_prefEdit, jni_mid_PrefCommit);
411
412     OIC_LOG(DEBUG, TAG, "OUT-CAManagerUpdatePrefStringSet");
413     return res;
414 }
415
416 jobject CAManagerGetPrefStringSet(JNIEnv *env, jobject context)
417 {
418     OIC_LOG(DEBUG, TAG, "CAManagerGetPrefStringSet");
419
420     VERIFY_NON_NULL_RET(env, TAG, "env", NULL);
421     VERIFY_NON_NULL_RET(context, TAG, "context", NULL);
422
423     jstring jni_str_prefName = (*env)->NewStringUTF(env, PREF_NAME_CONNECTED_DEVICE);
424     if (!jni_str_prefName)
425     {
426         OIC_LOG(ERROR, TAG, "jni_str_prefName is null");
427         return NULL;
428     }
429
430     jstring jni_str_prefKey = (*env)->NewStringUTF(env, PREF_KEY_CONNECTED_DEVICE);
431     if (!jni_str_prefKey)
432     {
433         OIC_LOG(ERROR, TAG, "jni_str_prefKey is null");
434         return NULL;
435     }
436
437     // get SharedPreference
438     jobject jni_obj_pref = CAManagerGetSharedPreference(env, context, jni_str_prefName);
439     if (!jni_obj_pref)
440     {
441         OIC_LOG(ERROR, TAG, "jni_obj_pref is null");
442         return NULL;
443     }
444
445     // contains(String key)
446     jobject jni_cls_pref = (*env)->FindClass(env, CLASSPATH_CONTENT_PREF);
447     if (!jni_cls_pref)
448     {
449         OIC_LOG(ERROR, TAG, "jni_cls_pref is null");
450         return NULL;
451     }
452
453     jmethodID jni_mid_getStringSet = (*env)->GetMethodID(env, jni_cls_pref, "getStringSet",
454                                                          "(Ljava/lang/String;Ljava/util/Set;)"
455                                                          "Ljava/util/Set;");
456     if (!jni_mid_getStringSet)
457     {
458         OIC_LOG(ERROR, TAG, "jni_mid_getStringSet is null");
459         return NULL;
460     }
461
462     jobject jni_defSet = CAManagerCreateSetString(env);
463     if (!jni_defSet)
464     {
465         OIC_LOG(ERROR, TAG, "jni_defSet is null");
466         return NULL;
467     }
468
469     jobject value = (*env)->CallObjectMethod(env, jni_obj_pref,
470                                              jni_mid_getStringSet,
471                                              jni_str_prefKey,
472                                              jni_defSet);
473
474     return value;
475 }
476
477 bool CAManagerContainsPrefStringSet(JNIEnv *env, jobject context)
478 {
479     OIC_LOG(DEBUG, TAG, "CAManagerContainsPrefStringSet");
480
481     VERIFY_NON_NULL_RET(env, TAG, "env", false);
482     VERIFY_NON_NULL_RET(context, TAG, "context", false);
483
484     jstring jni_str_prefName = (*env)->NewStringUTF(env, PREF_NAME_CONNECTED_DEVICE);
485     if (!jni_str_prefName)
486     {
487         OIC_LOG(ERROR, TAG, "jni_str_prefName is null");
488         return false;
489     }
490
491     jstring jni_str_prefKey = (*env)->NewStringUTF(env, PREF_KEY_CONNECTED_DEVICE);
492     if (!jni_str_prefKey)
493     {
494         OIC_LOG(ERROR, TAG, "jni_str_prefKey is null");
495         return false;
496     }
497
498     // get SharedPreference
499     jobject jni_obj_pref = CAManagerGetSharedPreference(env, context, jni_str_prefName);
500     if (!jni_obj_pref)
501     {
502         OIC_LOG(ERROR, TAG, "jni_obj_pref is null");
503         return false;
504     }
505
506     // contains(String key)
507     jobject jni_cls_pref = (*env)->FindClass(env, CLASSPATH_CONTENT_PREF);
508     if (!jni_cls_pref)
509     {
510         OIC_LOG(ERROR, TAG, "jni_cls_pref is null");
511         return false;
512     }
513
514     jmethodID jni_mid_prefContains = (*env)->GetMethodID(env, jni_cls_pref, "contains",
515                                                          METHODID_BOOLEAN_RET_STRING_PARAM);
516     if (!jni_mid_prefContains)
517     {
518         OIC_LOG(ERROR, TAG, "jni_mid_prefContains is null");
519         return false;
520     }
521
522     jboolean res = (*env)->CallBooleanMethod(env, jni_obj_pref,
523                                              jni_mid_prefContains,
524                                              jni_str_prefKey);
525
526     OIC_LOG_V(DEBUG, TAG, "Preference - contains (%d)", res);
527     return res;
528 }
529
530 bool CAManagerRemovePrefStringSet(JNIEnv *env, jobject context)
531 {
532     OIC_LOG(DEBUG, TAG, "CAManagerRemovePrefStringSet");
533
534     VERIFY_NON_NULL_RET(env, TAG, "env", false);
535     VERIFY_NON_NULL_RET(context, TAG, "context", false);
536
537     jstring jni_str_prefKey = (*env)->NewStringUTF(env, PREF_KEY_CONNECTED_DEVICE);
538     if (!jni_str_prefKey)
539     {
540         OIC_LOG(ERROR, TAG, "jni_str_prefKey is null");
541         return false;
542     }
543
544     jobject jni_obj_prefEdit = CAManagerGetPrefEditObject(env, context);
545     if (!jni_obj_prefEdit)
546     {
547         OIC_LOG(ERROR, TAG, "jni_obj_prefEdit is null");
548         return false;
549     }
550
551     // putString()
552     jobject jni_cls_edit = (*env)->FindClass(env, CLASSPATH_CONTENT_PREF_EDITOR);
553     if (!jni_cls_edit)
554     {
555         OIC_LOG(ERROR, TAG, "jni_cls_edit is null");
556         return false;
557     }
558
559     // get remove method interface
560     jmethodID jni_mid_PrefRemove = (*env)->GetMethodID(env, jni_cls_edit, "remove",
561                                                        "(Ljava/lang/String;)"
562                                                        "Landroid/content/SharedPreferences"
563                                                        "$Editor;");
564     if (!jni_mid_PrefRemove)
565     {
566         OIC_LOG(ERROR, TAG, "jni_mid_PrefRemove is null");
567         return false;
568     }
569
570     OIC_LOG(DEBUG, TAG, "CALL API - request remove for SharedPreferences.Editor");
571     (*env)->CallObjectMethod(env, jni_obj_prefEdit, jni_mid_PrefRemove, jni_str_prefKey);
572
573     // get commit method interface
574     jmethodID jni_mid_PrefCommit = (*env)->GetMethodID(env, jni_cls_edit, "commit",
575                                                        "()Z");
576     if (!jni_mid_PrefCommit)
577     {
578         OIC_LOG(ERROR, TAG, "jni_mid_PrefCommit is null");
579         return false;
580     }
581
582     jboolean res = (*env)->CallBooleanMethod(env, jni_obj_prefEdit, jni_mid_PrefCommit);
583
584     OIC_LOG(DEBUG, TAG, "OUT-CAManagerAddConnectedDeviceAddress");
585     return res;
586 }
587
588 bool CAManagerAddConnectedDeviceAddress(JNIEnv *env, jobject context,
589                                         jstring address, jobject set)
590 {
591     OIC_LOG(DEBUG, TAG, "IN-CAManagerAddConnectedDeviceAddress");
592
593     VERIFY_NON_NULL_RET(env, TAG, "env", false);
594     VERIFY_NON_NULL_RET(context, TAG, "context", false);
595     VERIFY_NON_NULL_RET(address, TAG, "address", false);
596     VERIFY_NON_NULL_RET(set, TAG, "set", false);
597
598     if (CAManagerCallFuncSetString(env, address, set, CM_CONTAINS))
599     {
600         OIC_LOG(DEBUG, TAG, "it's already done");
601         return true;
602     }
603
604     if (!CAManagerCallFuncSetString(env, address, set, CM_ADD))
605     {
606         OIC_LOG(ERROR, TAG, "CAManagerAddSetString has failed");
607         return false;
608     }
609
610     return CAManagerUpdatePrefStringSet(env, context, set);
611 }
612
613 bool CAManagerIsConnectedDeviceAddress(JNIEnv *env, jobject context,
614                                        jstring address, jobject set)
615 {
616     OIC_LOG(DEBUG, TAG, "CAManagerIsConnectedDeviceAddress");
617     VERIFY_NON_NULL_RET(env, TAG, "env", false);
618     VERIFY_NON_NULL_RET(context, TAG, "context", false);
619
620     return CAManagerCallFuncSetString(env, address, set, CM_CONTAINS);
621 }
622
623 jobject CAManagerGetConnectedDeviceAddress(JNIEnv *env, jobject context)
624 {
625     OIC_LOG(DEBUG, TAG, "CAManagerGetConnectedDeviceAddress");
626
627     if (!CAManagerContainsPrefStringSet(env, context))
628     {
629         OIC_LOG(DEBUG, TAG, "there is no set data");
630         return NULL;
631     }
632
633     return CAManagerGetPrefStringSet(env, context);
634 }
635
636 bool CAManagerRemoveConnectedDeviceAddress(JNIEnv *env, jobject context,
637                                            jstring address, jobject set)
638 {
639     OIC_LOG(DEBUG, TAG, "CAManagerRemoveConnectedDeviceAddress");
640
641     VERIFY_NON_NULL_RET(env, TAG, "env", false);
642     VERIFY_NON_NULL_RET(context, TAG, "context", false);
643     VERIFY_NON_NULL_RET(address, TAG, "address", false);
644     VERIFY_NON_NULL_RET(set, TAG, "set", false);
645
646     if (!CAManagerCallFuncSetString(env, address, set, CM_CONTAINS))
647     {
648         OIC_LOG(DEBUG, TAG, "it's already done");
649         return true;
650     }
651
652     if (!CAManagerCallFuncSetString(env, address, set, CM_REMOVE))
653     {
654         OIC_LOG(ERROR, TAG, "CAManagerAddSetString has failed");
655         return false;
656     }
657
658     return CAManagerUpdatePrefStringSet(env, context, set);
659 }
660
661 jboolean CAManagerCheckBTAddress(JNIEnv *env, jstring address)
662 {
663     OIC_LOG(DEBUG, TAG, "CAManagerCheckBTAddress");
664
665     VERIFY_NON_NULL_RET(env, TAG, "env is null", JNI_FALSE);
666     VERIFY_NON_NULL_RET(address, TAG, "address is null", JNI_FALSE);
667
668     jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
669     if (!jni_cid_BTAdapter)
670     {
671         OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null");
672         return JNI_FALSE;
673     }
674
675     // get remote bt adapter method
676     jmethodID jni_mid_checkBTAddress =
677             (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
678                                       "checkBluetoothAddress",
679                                       METHODID_BOOLEAN_RET_STRING_PARAM);
680     if (!jni_mid_checkBTAddress)
681     {
682         OIC_LOG(ERROR, TAG, "jni_mid_checkBTAddress is null");
683         return JNI_FALSE;
684     }
685
686     jboolean jni_obj_isAddress = (*env)->CallStaticBooleanMethod(env, jni_cid_BTAdapter,
687                                                                  jni_mid_checkBTAddress,
688                                                                  address);
689     return jni_obj_isAddress;
690 }
691
692 jobject CAManagerCreateSetString(JNIEnv *env)
693 {
694     OIC_LOG(DEBUG, TAG, "CAManagerCallFuncSetString");
695
696     VERIFY_NON_NULL_RET(env, TAG, "env", NULL);
697
698     jclass jclazzMap = (*env)->FindClass(env, "java/util/HashSet" );
699     if (!jclazzMap)
700     {
701         OIC_LOG(ERROR, TAG, "jclazzMap is null");
702         return NULL;
703     }
704
705     jmethodID jinitMap = (*env)->GetMethodID(env, jclazzMap, "<init>", "()V");
706     if (!jinitMap)
707     {
708         OIC_LOG(ERROR, TAG, "jinitMap is null");
709         return NULL;
710     }
711
712     jobject jpropertyMap = (*env)->NewObject(env, jclazzMap, jinitMap);
713     if (!jpropertyMap)
714     {
715         OIC_LOG(ERROR, TAG, "jpropertyMap is null");
716         return NULL;
717     }
718
719     return jpropertyMap;
720 }
721
722 bool CAManagerCallFuncSetString(JNIEnv *env, jstring address, jobject set,
723                                 CASetMethod_t method_type)
724 {
725     OIC_LOG(DEBUG, TAG, "CAManagerCallFuncSetString");
726     VERIFY_NON_NULL_RET(env, TAG, "env", false);
727     VERIFY_NON_NULL_RET(address, TAG, "address", false);
728     VERIFY_NON_NULL_RET(set, TAG, "set", false);
729
730     jclass jni_cls_set = (*env)->FindClass(env, "java/util/HashSet");
731     if (!jni_cls_set)
732     {
733         OIC_LOG(ERROR, TAG, "jni_cls_set is null");
734         return false;
735     }
736
737     jmethodID jni_mid_setMethod = NULL;
738     switch (method_type)
739     {
740         case CM_CONTAINS:
741             OIC_LOG(DEBUG, TAG, "java/util/HashSet.contains");
742             jni_mid_setMethod = (*env)->GetMethodID(env, jni_cls_set, "contains",
743                                                     "(Ljava/lang/Object;)Z");
744             break;
745         case CM_ADD:
746             OIC_LOG(DEBUG, TAG, "java/util/HashSet.add");
747             jni_mid_setMethod = (*env)->GetMethodID(env, jni_cls_set, "add",
748                                                     "(Ljava/lang/Object;)Z");
749             break;
750         case CM_REMOVE:
751             OIC_LOG(DEBUG, TAG, "java/util/HashSet.remove");
752             jni_mid_setMethod = (*env)->GetMethodID(env, jni_cls_set, "remove",
753                                                     "(Ljava/lang/Object;)Z");
754             break;
755         default:
756             break;
757     }
758     if (!jni_mid_setMethod)
759     {
760         OIC_LOG(ERROR, TAG, "jni_mid_setMethod is null");
761         return false;
762     }
763
764     jboolean res = (*env)->CallBooleanMethod(env, set, jni_mid_setMethod, address);
765     OIC_LOG_V(DEBUG, TAG, "method return result : %d", res);
766     return res;
767 }