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 jobject CALEGetParcelUuidFromString(JNIEnv *env, const char* uuid)
111 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
112 VERIFY_NON_NULL_RET(uuid, TAG, "uuid is null", NULL);
114 jclass jni_cid_ParcelUuid = (*env)->FindClass(env, "android/os/ParcelUuid");
115 if (!jni_cid_ParcelUuid)
117 OIC_LOG(ERROR, TAG, "jni_cid_ParcelUuid is not available");
121 jmethodID jni_mid_fromString = (*env)->GetStaticMethodID(env, jni_cid_ParcelUuid,
123 "(Ljava/lang/String;)"
124 "Landroid/os/ParcelUuid;");
125 if (!jni_mid_fromString)
127 OIC_LOG(ERROR, TAG, "jni_mid_fromString is not available");
131 jstring str_uuid = (*env)->NewStringUTF(env, uuid);
134 OIC_LOG(ERROR, TAG, "str_uuid is not available");
138 jobject jni_obj_parcelUuid = (*env)->CallStaticObjectMethod(env, jni_cid_ParcelUuid,
141 if (!jni_obj_parcelUuid)
143 OIC_LOG(ERROR, TAG, "Fail to get jni uuid object");
147 return jni_obj_parcelUuid;
149 CACheckJNIException(env);
153 bool CALEIsBondedDevice(JNIEnv *env, jobject bluetoothDevice)
155 VERIFY_NON_NULL_RET(env, TAG, "env is null", false);
156 VERIFY_NON_NULL_RET(bluetoothDevice, TAG, "bluetoothDevice is null", false);
158 jmethodID jni_mid_getBondState = CAGetJNIMethodID(env, "android/bluetooth/BluetoothDevice",
161 if (!jni_mid_getBondState)
163 OIC_LOG(ERROR, TAG, "jni_mid_getBondState is null");
167 jint jni_bondState = (jint)(*env)->CallIntMethod(env, bluetoothDevice, jni_mid_getBondState);
168 CACheckJNIException(env);
169 OIC_LOG_V(DEBUG, TAG, "bond state is %d", jni_bondState);
171 if (BOND_BONDED == jni_bondState)
173 OIC_LOG(DEBUG, TAG, "remote device is bonded");
178 OIC_LOG(DEBUG, TAG, "remote device is not bonded");
185 jobjectArray CALEGetBondedDevices(JNIEnv *env)
187 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
189 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
190 if (!jni_cid_BTAdapter)
192 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_cid_BTAdapter is null");
196 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
198 METHODID_OBJECTNONPARAM);
199 CACheckJNIException(env);
200 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
201 jni_mid_getDefaultAdapter);
202 if (!jni_obj_BTAdapter)
204 OIC_LOG(ERROR, TAG, "getBondedDevices: bluetooth adapter is null");
208 // Get a list of currently paired devices
209 jmethodID jni_mid_getBondedDevices = (*env)->GetMethodID(env, jni_cid_BTAdapter,
211 "()Ljava/util/Set;");
212 if (!jni_mid_getBondedDevices)
214 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_mid_getBondedDevicesr is null");
218 jobject jni_obj_setPairedDevices = (*env)->CallObjectMethod(env, jni_obj_BTAdapter,
219 jni_mid_getBondedDevices);
220 if (!jni_obj_setPairedDevices)
222 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_obj_setPairedDevices is null");
226 jmethodID jni_mid_toArray = CAGetJNIMethodID(env, "java/util/Set", "toArray",
227 "()[Ljava/lang/Object;");
228 if (!jni_mid_toArray)
230 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_mid_toArray is null");
234 jobjectArray jni_arrayPairedDevices = (jobjectArray)(
235 (*env)->CallObjectMethod(env, jni_obj_setPairedDevices, jni_mid_toArray));
236 if (!jni_arrayPairedDevices)
238 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_arrayPairedDevices is null");
242 return jni_arrayPairedDevices;
245 CACheckJNIException(env);
249 jint CALEGetBTStateOnInfo(JNIEnv *env)
251 VERIFY_NON_NULL_RET(env, TAG, "env is null", -1);
253 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
254 if (!jni_cid_BTAdapter)
256 OIC_LOG(ERROR, TAG, "getBTStateOnInfo: jni_cid_BTAdapter is null");
257 CACheckJNIException(env);
261 jfieldID jni_fid_stateon = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
262 if (!jni_fid_stateon)
264 OIC_LOG(ERROR, TAG, "get_field_state is not available");
265 CACheckJNIException(env);
269 jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, jni_fid_stateon);
270 CACheckJNIException(env);
271 OIC_LOG_V(DEBUG, TAG, "bluetooth.STATE_ON state integer value : %d", jni_int_val);
276 CAResult_t CALECheckPlatformVersion(JNIEnv *env, uint16_t level)
278 jint jni_int_sdk = CALEGetBuildVersion(env);
279 if (jni_int_sdk < level)
281 OIC_LOG(ERROR, TAG, "it is not supported");
282 return CA_NOT_SUPPORTED;
288 jint CALEGetBuildVersion(JNIEnv *env)
290 VERIFY_NON_NULL_RET(env, TAG, "env is null", -1);
292 // VERSION is a nested class within android.os.Build (hence "$" rather than "/")
293 jclass jni_cls_version = (*env)->FindClass(env, "android/os/Build$VERSION");
294 if (!jni_cls_version)
296 OIC_LOG(ERROR, TAG, "jni_cls_version is null");
297 CACheckJNIException(env);
301 jfieldID jni_fid_sdk = (*env)->GetStaticFieldID(env, jni_cls_version, "SDK_INT", "I");
304 OIC_LOG(ERROR, TAG, "jni_fid_sdk is null");
305 CACheckJNIException(env);
309 jint jni_int_sdk = (*env)->GetStaticIntField(env, jni_cls_version, jni_fid_sdk);
310 CACheckJNIException(env);
311 OIC_LOG_V(DEBUG, TAG, "sdk version is %d", jni_int_sdk);
316 jint CALEGetBuildVersionCodeForName(JNIEnv *env, const char* versionName)
318 VERIFY_NON_NULL_RET(env, TAG, "env is null", -1);
319 VERIFY_NON_NULL_RET(versionName, TAG, "versionName is null", -1);
321 // VERSION is a nested class within android.os.Build (hence "$" rather than "/")
322 jclass jni_cls_version = (*env)->FindClass(env, "android/os/Build$VERSION_CODES");
323 if (!jni_cls_version)
325 OIC_LOG(ERROR, TAG, "jni_cls_version is null");
326 CACheckJNIException(env);
330 jfieldID jni_fid_version = (*env)->GetStaticFieldID(env, jni_cls_version, versionName, "I");
331 if (!jni_fid_version)
333 OIC_LOG(ERROR, TAG, "jni_fid_version is null");
334 CACheckJNIException(env);
338 jint jni_int_version = (*env)->GetStaticIntField(env, jni_cls_version, jni_fid_version);
339 CACheckJNIException(env);
340 OIC_LOG_V(DEBUG, TAG, "version [%s] is %d",versionName, jni_int_version);
342 return jni_int_version;
345 jboolean CALEIsEnableBTAdapter(JNIEnv *env)
347 VERIFY_NON_NULL_RET(env, TAG, "env is null", JNI_FALSE);
349 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
350 if (!jni_cid_BTAdapter)
352 OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter: jni_cid_BTAdapter is null");
353 CACheckJNIException(env);
357 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
359 METHODID_OBJECTNONPARAM);
360 if (!jni_mid_getDefaultAdapter)
362 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
363 CACheckJNIException(env);
364 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
368 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
369 jni_mid_getDefaultAdapter);
370 if (!jni_obj_BTAdapter)
372 OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
373 CACheckJNIException(env);
374 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
379 jmethodID jni_mid_isEnable = (*env)->GetMethodID(env, jni_cid_BTAdapter, "isEnabled", "()Z");
380 if (!jni_mid_isEnable)
382 OIC_LOG(ERROR, TAG, "jni_mid_isEnable is null");
383 CACheckJNIException(env);
384 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
385 (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
389 jboolean jni_isEnable = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_isEnable);
390 CACheckJNIException(env);
391 OIC_LOG_V(DEBUG, TAG, "adapter state is %d", jni_isEnable);
393 (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
394 (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
398 jstring CALEGetAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice)
400 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
401 VERIFY_NON_NULL_RET(bluetoothDevice, TAG, "bluetoothDevice is null", NULL);
403 jmethodID jni_mid_getAddress = CAGetJNIMethodID(env, "android/bluetooth/BluetoothDevice",
405 "()Ljava/lang/String;");
406 if (!jni_mid_getAddress)
408 OIC_LOG(ERROR, TAG, "jni_mid_getAddress is null");
412 jstring jni_address = (jstring)(*env)->CallObjectMethod(env, bluetoothDevice,
416 OIC_LOG(ERROR, TAG, "jni_address is null");
417 CACheckJNIException(env);
424 jint CALEGetConstantsValue(JNIEnv *env, const char* classType, const char* name)
426 VERIFY_NON_NULL_RET(env, TAG, "env", -1);
427 VERIFY_NON_NULL_RET(classType, TAG, "classType", -1);
428 VERIFY_NON_NULL_RET(name, TAG, "name", -1);
430 jclass jni_cid = (*env)->FindClass(env, classType);
433 OIC_LOG(ERROR, TAG, "jni_cid is null");
434 CACheckJNIException(env);
438 jfieldID jni_fieldID = (*env)->GetStaticFieldID(env, jni_cid,
442 OIC_LOG(ERROR, TAG, "jni_fieldID is null");
443 CACheckJNIException(env);
447 jint jni_id = (*env)->GetStaticIntField(env, jni_cid, jni_fieldID);
448 CACheckJNIException(env);
452 jobject CALEGetRemoteDevice(JNIEnv *env, jstring address)
454 OIC_LOG(DEBUG, TAG, "CALEGetRemoteDevice");
456 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
457 VERIFY_NON_NULL_RET(address, TAG, "address is null", NULL);
459 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
460 if (!jni_cid_BTAdapter)
462 OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null");
466 // get remote bt adapter method
467 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
469 METHODID_OBJECTNONPARAM);
470 if (!jni_mid_getDefaultAdapter)
472 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
476 // gat bt adapter object
477 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
478 jni_mid_getDefaultAdapter);
479 if (!jni_obj_BTAdapter)
481 OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
485 jmethodID jni_mid_getRemoteDevice = (*env)->GetMethodID(env, jni_cid_BTAdapter,
487 METHODID_BT_REMOTE_DEVICE);
488 if (!jni_mid_getRemoteDevice)
490 OIC_LOG(ERROR, TAG, "jni_mid_getRemoteDevice is null");
494 jobject jni_obj_device = (*env)->CallObjectMethod(env, jni_obj_BTAdapter,
495 jni_mid_getRemoteDevice,
499 OIC_LOG(ERROR, TAG, "jni_obj_device is null");
502 return jni_obj_device;
505 CACheckJNIException(env);
509 jstring CALEGetAddressFromGatt(JNIEnv *env, jobject gatt)
511 OIC_LOG(DEBUG, TAG, "CALEGetAddressFromGatt");
513 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
514 VERIFY_NON_NULL_RET(gatt, TAG, "gatt is null", NULL);
516 jmethodID jni_mid_getDevice = CAGetJNIMethodID(env, CLASSPATH_BT_GATT, "getDevice",
518 if (!jni_mid_getDevice)
520 OIC_LOG(ERROR, TAG, "jni_mid_getDevice is null");
524 jobject jni_obj_device = (*env)->CallObjectMethod(env, gatt, jni_mid_getDevice);
527 OIC_LOG(ERROR, TAG, "jni_obj_device is null");
528 CACheckJNIException(env);
532 jstring jni_address = CALEGetAddressFromBTDevice(env, jni_obj_device);
535 OIC_LOG(ERROR, TAG, "jni_address is null");