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 jmethodID CALEGetJNIMethodID(JNIEnv *env, const char* className,
34 const char* methodName,
35 const char* methodFormat)
37 VERIFY_NON_NULL_RET(env, TAG, "env", NULL);
38 VERIFY_NON_NULL_RET(className, TAG, "className", NULL);
39 VERIFY_NON_NULL_RET(methodName, TAG, "methodName", NULL);
40 VERIFY_NON_NULL_RET(methodFormat, TAG, "methodFormat", NULL);
42 jclass jni_cid = (*env)->FindClass(env, className);
45 OIC_LOG_V(ERROR, TAG, "jni_cid [%s] is null", className);
49 jmethodID jni_midID = (*env)->GetMethodID(env, jni_cid, methodName, methodFormat);
52 OIC_LOG_V(ERROR, TAG, "jni_midID [%s] is null", methodName);
59 jobject CALEGetUuidFromString(JNIEnv *env, const char* uuid)
61 VERIFY_NON_NULL_RET(uuid, TAG, "uuid is null", NULL);
62 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
64 jclass jni_cid_UUID = (*env)->FindClass(env, "java/util/UUID");
67 OIC_LOG(ERROR, TAG, "jni_cid_UUID is not available");
71 jmethodID jni_mid_fromString = (*env)->GetStaticMethodID(env, jni_cid_UUID, "fromString",
72 "(Ljava/lang/String;)"
74 if (!jni_mid_fromString)
76 OIC_LOG(ERROR, TAG, "jni_mid_fromString is not available");
80 jstring str_uuid = (*env)->NewStringUTF(env, uuid);
83 OIC_LOG(ERROR, TAG, "str_uuid is not available");
87 jobject jni_obj_uuid = (*env)->CallStaticObjectMethod(env, jni_cid_UUID, jni_mid_fromString,
91 OIC_LOG(ERROR, TAG, "Fail to get jni uuid object");
98 jobject CALEGetParcelUuid(JNIEnv *env, jobject uuid)
100 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
101 VERIFY_NON_NULL_RET(uuid, TAG, "uuid is null", NULL);
103 jclass jni_cid_ParcelUuid = (*env)->FindClass(env, "android/os/ParcelUuid");
104 if (!jni_cid_ParcelUuid)
106 OIC_LOG(ERROR, TAG, "jni_cid_ParcelUuid is not available");
110 jmethodID jni_mid_ParcelUuid = (*env)->GetMethodID(env, jni_cid_ParcelUuid, "<init>",
111 "(Ljava/util/UUID;)V");
112 if (!jni_mid_ParcelUuid)
114 OIC_LOG(ERROR, TAG, "jni_mid_ParcelUuid is not available");
118 jobject jni_ParcelUuid = (*env)->NewObject(env, jni_cid_ParcelUuid, jni_mid_ParcelUuid, uuid);
121 OIC_LOG(ERROR, TAG, "Fail to get jni ParcelUuid");
125 return jni_ParcelUuid;
128 bool CALEIsBondedDevice(JNIEnv *env, jobject bluetoothDevice)
130 VERIFY_NON_NULL_RET(env, TAG, "env is null", false);
131 VERIFY_NON_NULL_RET(bluetoothDevice, TAG, "bluetoothDevice is null", false);
133 jmethodID jni_mid_getBondState = CALEGetJNIMethodID(env, "android/bluetooth/BluetoothDevice",
136 if (!jni_mid_getBondState)
138 OIC_LOG(ERROR, TAG, "jni_mid_getBondState is null");
142 jint jni_bondState = (jint)(*env)->CallIntMethod(env, bluetoothDevice, jni_mid_getBondState);
144 OIC_LOG_V(DEBUG, TAG, "bond state is %d", jni_bondState);
146 if (BOND_BONDED == jni_bondState)
148 OIC_LOG(DEBUG, TAG, "remote device is bonded");
153 OIC_LOG(DEBUG, TAG, "remote device is not bonded");
160 jobjectArray CALEGetBondedDevices(JNIEnv *env)
162 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
164 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
165 if (!jni_cid_BTAdapter)
167 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_cid_BTAdapter is null");
171 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
173 METHODID_OBJECTNONPARAM);
175 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
176 jni_mid_getDefaultAdapter);
177 if (!jni_obj_BTAdapter)
179 OIC_LOG(ERROR, TAG, "getBondedDevices: bluetooth adapter is null");
183 // Get a list of currently paired devices
184 jmethodID jni_mid_getBondedDevices = (*env)->GetMethodID(env, jni_cid_BTAdapter,
186 "()Ljava/util/Set;");
187 if (!jni_mid_getBondedDevices)
189 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_mid_getBondedDevicesr is null");
193 jobject jni_obj_setPairedDevices = (*env)->CallObjectMethod(env, jni_obj_BTAdapter,
194 jni_mid_getBondedDevices);
195 if (!jni_obj_setPairedDevices)
197 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_obj_setPairedDevices is null");
201 jmethodID jni_mid_toArray = CALEGetJNIMethodID(env, "java/util/Set", "toArray",
202 "()[Ljava/lang/Object;");
203 if (!jni_mid_toArray)
205 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_mid_toArray is null");
209 jobjectArray jni_arrayPairedDevices = (jobjectArray)(
210 (*env)->CallObjectMethod(env, jni_obj_setPairedDevices, jni_mid_toArray));
211 if (!jni_arrayPairedDevices)
213 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_arrayPairedDevices is null");
217 return jni_arrayPairedDevices;
220 jint CALEGetBTStateOnInfo(JNIEnv *env)
222 VERIFY_NON_NULL_RET(env, TAG, "env is null", -1);
224 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
225 if (!jni_cid_BTAdapter)
227 OIC_LOG(ERROR, TAG, "getBTStateOnInfo: jni_cid_BTAdapter is null");
231 jfieldID jni_fid_stateon = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
232 if (!jni_fid_stateon)
234 OIC_LOG(ERROR, TAG, "get_field_state is not available");
238 jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, jni_fid_stateon);
239 OIC_LOG_V(DEBUG, TAG, "bluetooth.STATE_ON state integer value : %d", jni_int_val);
244 CAResult_t CALECheckPlatformVersion(JNIEnv *env, uint16_t level)
246 jint jni_int_sdk = CALEGetBuildVersion(env);
247 if (jni_int_sdk < level)
249 OIC_LOG(ERROR, TAG, "it is not supported");
250 return CA_NOT_SUPPORTED;
256 jint CALEGetBuildVersion(JNIEnv *env)
258 VERIFY_NON_NULL_RET(env, TAG, "env is null", -1);
260 // VERSION is a nested class within android.os.Build (hence "$" rather than "/")
261 jclass jni_cls_version = (*env)->FindClass(env, "android/os/Build$VERSION");
262 if (!jni_cls_version)
264 OIC_LOG(ERROR, TAG, "jni_cls_version is null");
268 jfieldID jni_fid_sdk = (*env)->GetStaticFieldID(env, jni_cls_version, "SDK_INT", "I");
271 OIC_LOG(ERROR, TAG, "jni_fid_sdk is null");
275 jint jni_int_sdk = (*env)->GetStaticIntField(env, jni_cls_version, jni_fid_sdk);
276 OIC_LOG_V(DEBUG, TAG, "sdk version is %d", jni_int_sdk);
281 jint CALEGetBuildVersionCodeForName(JNIEnv *env, const char* versionName)
283 VERIFY_NON_NULL_RET(env, TAG, "env is null", -1);
284 VERIFY_NON_NULL_RET(versionName, TAG, "versionName is null", -1);
286 // VERSION is a nested class within android.os.Build (hence "$" rather than "/")
287 jclass jni_cls_version = (*env)->FindClass(env, "android/os/Build$VERSION_CODES");
288 if (!jni_cls_version)
290 OIC_LOG(ERROR, TAG, "jni_cls_version is null");
294 jfieldID jni_fid_version = (*env)->GetStaticFieldID(env, jni_cls_version, versionName, "I");
295 if (!jni_fid_version)
297 OIC_LOG(ERROR, TAG, "jni_fid_version is null");
301 jint jni_int_version = (*env)->GetStaticIntField(env, jni_cls_version, jni_fid_version);
302 OIC_LOG_V(DEBUG, TAG, "version [%s] is %d",versionName, jni_int_version);
304 return jni_int_version;
307 jboolean CALEIsEnableBTAdapter(JNIEnv *env)
309 VERIFY_NON_NULL_RET(env, TAG, "env is null", JNI_FALSE);
311 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
312 if (!jni_cid_BTAdapter)
314 OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter: jni_cid_BTAdapter is null");
318 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
320 METHODID_OBJECTNONPARAM);
321 if (!jni_mid_getDefaultAdapter)
323 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
327 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
328 jni_mid_getDefaultAdapter);
329 if (!jni_obj_BTAdapter)
331 OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
336 jmethodID jni_mid_isEnable = (*env)->GetMethodID(env, jni_cid_BTAdapter, "isEnabled", "()Z");
337 if (!jni_mid_isEnable)
339 OIC_LOG(ERROR, TAG, "jni_mid_isEnable is null");
343 jboolean jni_isEnable = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_isEnable);
344 OIC_LOG_V(DEBUG, TAG, "adapter state is %d", jni_isEnable);
349 jstring CALEGetAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice)
351 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
352 VERIFY_NON_NULL_RET(bluetoothDevice, TAG, "bluetoothDevice is null", NULL);
354 jmethodID jni_mid_getAddress = CALEGetJNIMethodID(env, "android/bluetooth/BluetoothDevice",
356 "()Ljava/lang/String;");
357 if (!jni_mid_getAddress)
359 OIC_LOG(ERROR, TAG, "jni_mid_getAddress is null");
363 jstring jni_address = (jstring)(*env)->CallObjectMethod(env, bluetoothDevice,
367 OIC_LOG(ERROR, TAG, "jni_address is null");
374 jint CALEGetConstantsValue(JNIEnv *env, const char* classType, const char* name)
376 OIC_LOG(DEBUG, TAG, "CALEGetConstantsValue");
378 VERIFY_NON_NULL_RET(env, TAG, "env", -1);
379 VERIFY_NON_NULL_RET(classType, TAG, "classType", -1);
380 VERIFY_NON_NULL_RET(name, TAG, "name", -1);
382 jclass jni_cid = (*env)->FindClass(env, classType);
385 OIC_LOG(ERROR, TAG, "jni_cid is null");
389 jfieldID jni_fieldID = (*env)->GetStaticFieldID(env, jni_cid,
393 OIC_LOG(ERROR, TAG, "jni_fieldID is null");
397 return (*env)->GetStaticIntField(env, jni_cid, jni_fieldID);
400 jobject CALEGetRemoteDevice(JNIEnv *env, jstring address)
402 OIC_LOG(DEBUG, TAG, "IN - CALEGetRemoteDevice");
404 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
405 VERIFY_NON_NULL_RET(address, TAG, "address is null", NULL);
407 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
408 if (!jni_cid_BTAdapter)
410 OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null");
414 // get remote bt adapter method
415 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
417 METHODID_OBJECTNONPARAM);
418 if (!jni_mid_getDefaultAdapter)
420 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
424 // gat bt adapter object
425 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
426 jni_mid_getDefaultAdapter);
427 if (!jni_obj_BTAdapter)
429 OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
433 jmethodID jni_mid_getRemoteDevice = (*env)->GetMethodID(env, jni_cid_BTAdapter,
435 METHODID_BT_REMOTE_DEVICE);
436 if (!jni_mid_getRemoteDevice)
438 OIC_LOG(ERROR, TAG, "jni_mid_getRemoteDevice is null");
442 jobject jni_obj_device = (*env)->CallObjectMethod(env, jni_obj_BTAdapter,
443 jni_mid_getRemoteDevice,
447 OIC_LOG(ERROR, TAG, "jni_obj_device is null");
451 OIC_LOG(DEBUG, TAG, "OUT - CALEGetRemoteDevice");
452 return jni_obj_device;
455 jstring CALEGetAddressFromGatt(JNIEnv *env, jobject gatt)
457 OIC_LOG(DEBUG, TAG, "IN - CALEGetAddressFromGatt");
459 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
460 VERIFY_NON_NULL_RET(gatt, TAG, "gatt is null", NULL);
462 jmethodID jni_mid_getDevice = CALEGetJNIMethodID(env, CLASSPATH_BT_GATT, "getDevice",
464 if (!jni_mid_getDevice)
466 OIC_LOG(ERROR, TAG, "jni_mid_getDevice is null");
470 jobject jni_obj_device = (*env)->CallObjectMethod(env, gatt, jni_mid_getDevice);
473 OIC_LOG(ERROR, TAG, "jni_obj_device is null");
477 jstring jni_address = CALEGetAddressFromBTDevice(env, jni_obj_device);
480 OIC_LOG(ERROR, TAG, "jni_address is null");
484 OIC_LOG(DEBUG, TAG, "OUT - CALEGetAddressFromGatt");