1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
23 #include <android/log.h>
25 #include "caleutils.h"
27 #include "cathreadpool.h"
28 #include "uarraylist.h"
29 #include "caadapterutils.h"
31 #define TAG PCF("OIC_CA_LE_UTILS")
33 jobject CALEGetUuidFromString(JNIEnv *env, const char* uuid)
35 VERIFY_NON_NULL_RET(uuid, TAG, "uuid is null", NULL);
36 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
38 jclass jni_cid_UUID = (*env)->FindClass(env, "java/util/UUID");
41 OIC_LOG(ERROR, TAG, "jni_cid_UUID is not available");
45 jmethodID jni_mid_fromString = (*env)->GetStaticMethodID(env, jni_cid_UUID, "fromString",
46 "(Ljava/lang/String;)"
48 if (!jni_mid_fromString)
50 OIC_LOG(ERROR, TAG, "jni_mid_fromString is not available");
54 jstring str_uuid = (*env)->NewStringUTF(env, uuid);
57 OIC_LOG(ERROR, TAG, "str_uuid is not available");
61 jobject jni_obj_uuid = (*env)->CallStaticObjectMethod(env, jni_cid_UUID, jni_mid_fromString,
65 OIC_LOG(ERROR, TAG, "Fail to get jni uuid object");
72 CACheckJNIException(env);
76 jobject CALEGetParcelUuid(JNIEnv *env, jobject uuid)
78 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
79 VERIFY_NON_NULL_RET(uuid, TAG, "uuid is null", NULL);
81 jclass jni_cid_ParcelUuid = (*env)->FindClass(env, "android/os/ParcelUuid");
82 if (!jni_cid_ParcelUuid)
84 OIC_LOG(ERROR, TAG, "jni_cid_ParcelUuid is not available");
88 jmethodID jni_mid_ParcelUuid = (*env)->GetMethodID(env, jni_cid_ParcelUuid, "<init>",
89 "(Ljava/util/UUID;)V");
90 if (!jni_mid_ParcelUuid)
92 OIC_LOG(ERROR, TAG, "jni_mid_ParcelUuid is not available");
96 jobject jni_ParcelUuid = (*env)->NewObject(env, jni_cid_ParcelUuid, jni_mid_ParcelUuid, uuid);
99 OIC_LOG(ERROR, TAG, "Fail to get jni ParcelUuid");
103 return jni_ParcelUuid;
105 CACheckJNIException(env);
109 bool CALEIsBondedDevice(JNIEnv *env, jobject bluetoothDevice)
111 VERIFY_NON_NULL_RET(env, TAG, "env is null", false);
112 VERIFY_NON_NULL_RET(bluetoothDevice, TAG, "bluetoothDevice is null", false);
114 jmethodID jni_mid_getBondState = CAGetJNIMethodID(env, "android/bluetooth/BluetoothDevice",
117 if (!jni_mid_getBondState)
119 OIC_LOG(ERROR, TAG, "jni_mid_getBondState is null");
123 jint jni_bondState = (jint)(*env)->CallIntMethod(env, bluetoothDevice, jni_mid_getBondState);
124 CACheckJNIException(env);
125 OIC_LOG_V(DEBUG, TAG, "bond state is %d", jni_bondState);
127 if (BOND_BONDED == jni_bondState)
129 OIC_LOG(DEBUG, TAG, "remote device is bonded");
134 OIC_LOG(DEBUG, TAG, "remote device is not bonded");
139 jobjectArray CALEGetBondedDevices(JNIEnv *env)
141 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
143 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
144 if (!jni_cid_BTAdapter)
146 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_cid_BTAdapter is null");
150 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
152 METHODID_OBJECTNONPARAM);
153 CACheckJNIException(env);
154 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
155 jni_mid_getDefaultAdapter);
156 if (!jni_obj_BTAdapter)
158 OIC_LOG(ERROR, TAG, "getBondedDevices: bluetooth adapter is null");
162 // Get a list of currently paired devices
163 jmethodID jni_mid_getBondedDevices = (*env)->GetMethodID(env, jni_cid_BTAdapter,
165 "()Ljava/util/Set;");
166 if (!jni_mid_getBondedDevices)
168 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_mid_getBondedDevicesr is null");
172 jobject jni_obj_setPairedDevices = (*env)->CallObjectMethod(env, jni_obj_BTAdapter,
173 jni_mid_getBondedDevices);
174 if (!jni_obj_setPairedDevices)
176 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_obj_setPairedDevices is null");
180 jmethodID jni_mid_toArray = CAGetJNIMethodID(env, "java/util/Set", "toArray",
181 "()[Ljava/lang/Object;");
182 if (!jni_mid_toArray)
184 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_mid_toArray is null");
188 jobjectArray jni_arrayPairedDevices = (jobjectArray)(
189 (*env)->CallObjectMethod(env, jni_obj_setPairedDevices, jni_mid_toArray));
190 if (!jni_arrayPairedDevices)
192 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_arrayPairedDevices is null");
196 return jni_arrayPairedDevices;
199 CACheckJNIException(env);
203 jint CALEGetBTStateOnInfo(JNIEnv *env)
205 VERIFY_NON_NULL_RET(env, TAG, "env is null", -1);
207 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
208 if (!jni_cid_BTAdapter)
210 OIC_LOG(ERROR, TAG, "getBTStateOnInfo: jni_cid_BTAdapter is null");
211 CACheckJNIException(env);
215 jfieldID jni_fid_stateon = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
216 if (!jni_fid_stateon)
218 OIC_LOG(ERROR, TAG, "get_field_state is not available");
219 CACheckJNIException(env);
223 jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, jni_fid_stateon);
224 CACheckJNIException(env);
225 OIC_LOG_V(DEBUG, TAG, "bluetooth.STATE_ON state integer value : %d", jni_int_val);
230 CAResult_t CALECheckPlatformVersion(JNIEnv *env, uint16_t level)
232 jint jni_int_sdk = CALEGetBuildVersion(env);
233 if (jni_int_sdk < level)
235 OIC_LOG(ERROR, TAG, "it is not supported");
236 return CA_NOT_SUPPORTED;
242 jint CALEGetBuildVersion(JNIEnv *env)
244 VERIFY_NON_NULL_RET(env, TAG, "env is null", -1);
246 // VERSION is a nested class within android.os.Build (hence "$" rather than "/")
247 jclass jni_cls_version = (*env)->FindClass(env, "android/os/Build$VERSION");
248 if (!jni_cls_version)
250 OIC_LOG(ERROR, TAG, "jni_cls_version is null");
251 CACheckJNIException(env);
255 jfieldID jni_fid_sdk = (*env)->GetStaticFieldID(env, jni_cls_version, "SDK_INT", "I");
258 OIC_LOG(ERROR, TAG, "jni_fid_sdk is null");
259 CACheckJNIException(env);
263 jint jni_int_sdk = (*env)->GetStaticIntField(env, jni_cls_version, jni_fid_sdk);
264 CACheckJNIException(env);
265 OIC_LOG_V(DEBUG, TAG, "sdk version is %d", jni_int_sdk);
270 jint CALEGetBuildVersionCodeForName(JNIEnv *env, const char* versionName)
272 VERIFY_NON_NULL_RET(env, TAG, "env is null", -1);
273 VERIFY_NON_NULL_RET(versionName, TAG, "versionName is null", -1);
275 // VERSION is a nested class within android.os.Build (hence "$" rather than "/")
276 jclass jni_cls_version = (*env)->FindClass(env, "android/os/Build$VERSION_CODES");
277 if (!jni_cls_version)
279 OIC_LOG(ERROR, TAG, "jni_cls_version is null");
280 CACheckJNIException(env);
284 jfieldID jni_fid_version = (*env)->GetStaticFieldID(env, jni_cls_version, versionName, "I");
285 if (!jni_fid_version)
287 OIC_LOG(ERROR, TAG, "jni_fid_version is null");
288 CACheckJNIException(env);
292 jint jni_int_version = (*env)->GetStaticIntField(env, jni_cls_version, jni_fid_version);
293 CACheckJNIException(env);
294 OIC_LOG_V(DEBUG, TAG, "version [%s] is %d",versionName, jni_int_version);
296 return jni_int_version;
299 jboolean CALEIsEnableBTAdapter(JNIEnv *env)
301 VERIFY_NON_NULL_RET(env, TAG, "env is null", JNI_FALSE);
303 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
304 if (!jni_cid_BTAdapter)
306 OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter: jni_cid_BTAdapter is null");
307 CACheckJNIException(env);
311 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
313 METHODID_OBJECTNONPARAM);
314 if (!jni_mid_getDefaultAdapter)
316 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
317 CACheckJNIException(env);
318 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
322 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
323 jni_mid_getDefaultAdapter);
324 if (!jni_obj_BTAdapter)
326 OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
327 CACheckJNIException(env);
328 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
333 jmethodID jni_mid_isEnable = (*env)->GetMethodID(env, jni_cid_BTAdapter, "isEnabled", "()Z");
334 if (!jni_mid_isEnable)
336 OIC_LOG(ERROR, TAG, "jni_mid_isEnable is null");
337 CACheckJNIException(env);
338 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
339 (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
343 jboolean jni_isEnable = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_isEnable);
344 CACheckJNIException(env);
345 OIC_LOG_V(DEBUG, TAG, "adapter state is %d", jni_isEnable);
347 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
348 (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
352 jstring CALEGetAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice)
354 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
355 VERIFY_NON_NULL_RET(bluetoothDevice, TAG, "bluetoothDevice is null", NULL);
357 jmethodID jni_mid_getAddress = CAGetJNIMethodID(env, "android/bluetooth/BluetoothDevice",
359 "()Ljava/lang/String;");
360 if (!jni_mid_getAddress)
362 OIC_LOG(ERROR, TAG, "jni_mid_getAddress is null");
366 jstring jni_address = (jstring)(*env)->CallObjectMethod(env, bluetoothDevice,
370 OIC_LOG(ERROR, TAG, "jni_address is null");
371 CACheckJNIException(env);
378 jint CALEGetConstantsValue(JNIEnv *env, const char* classType, const char* name)
380 VERIFY_NON_NULL_RET(env, TAG, "env", -1);
381 VERIFY_NON_NULL_RET(classType, TAG, "classType", -1);
382 VERIFY_NON_NULL_RET(name, TAG, "name", -1);
384 jclass jni_cid = (*env)->FindClass(env, classType);
387 OIC_LOG(ERROR, TAG, "jni_cid is null");
388 CACheckJNIException(env);
392 jfieldID jni_fieldID = (*env)->GetStaticFieldID(env, jni_cid,
396 OIC_LOG(ERROR, TAG, "jni_fieldID is null");
397 CACheckJNIException(env);
401 jint jni_id = (*env)->GetStaticIntField(env, jni_cid, jni_fieldID);
402 CACheckJNIException(env);
406 jobject CALEGetRemoteDevice(JNIEnv *env, jstring address)
408 OIC_LOG(DEBUG, TAG, "IN - CALEGetRemoteDevice");
410 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
411 VERIFY_NON_NULL_RET(address, TAG, "address is null", NULL);
413 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
414 if (!jni_cid_BTAdapter)
416 OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null");
420 // get remote bt adapter method
421 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
423 METHODID_OBJECTNONPARAM);
424 if (!jni_mid_getDefaultAdapter)
426 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
430 // gat bt adapter object
431 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
432 jni_mid_getDefaultAdapter);
433 if (!jni_obj_BTAdapter)
435 OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
439 jmethodID jni_mid_getRemoteDevice = (*env)->GetMethodID(env, jni_cid_BTAdapter,
441 METHODID_BT_REMOTE_DEVICE);
442 if (!jni_mid_getRemoteDevice)
444 OIC_LOG(ERROR, TAG, "jni_mid_getRemoteDevice is null");
448 jobject jni_obj_device = (*env)->CallObjectMethod(env, jni_obj_BTAdapter,
449 jni_mid_getRemoteDevice,
453 OIC_LOG(ERROR, TAG, "jni_obj_device is null");
457 OIC_LOG(DEBUG, TAG, "OUT - CALEGetRemoteDevice");
458 return jni_obj_device;
461 CACheckJNIException(env);
465 jstring CALEGetAddressFromGatt(JNIEnv *env, jobject gatt)
467 OIC_LOG(DEBUG, TAG, "IN - CALEGetAddressFromGatt");
469 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
470 VERIFY_NON_NULL_RET(gatt, TAG, "gatt is null", NULL);
472 jmethodID jni_mid_getDevice = CAGetJNIMethodID(env, CLASSPATH_BT_GATT, "getDevice",
474 if (!jni_mid_getDevice)
476 OIC_LOG(ERROR, TAG, "jni_mid_getDevice is null");
480 jobject jni_obj_device = (*env)->CallObjectMethod(env, gatt, jni_mid_getDevice);
483 OIC_LOG(ERROR, TAG, "jni_obj_device is null");
484 CACheckJNIException(env);
488 jstring jni_address = CALEGetAddressFromBTDevice(env, jni_obj_device);
491 OIC_LOG(ERROR, TAG, "jni_address is null");
495 OIC_LOG(DEBUG, TAG, "OUT - CALEGetAddressFromGatt");