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 jobject CALEGetParcelUuid(JNIEnv *env, jobject uuid)
74 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
75 VERIFY_NON_NULL_RET(uuid, TAG, "uuid is null", NULL);
77 jclass jni_cid_ParcelUuid = (*env)->FindClass(env, "android/os/ParcelUuid");
78 if (!jni_cid_ParcelUuid)
80 OIC_LOG(ERROR, TAG, "jni_cid_ParcelUuid is not available");
84 jmethodID jni_mid_ParcelUuid = (*env)->GetMethodID(env, jni_cid_ParcelUuid, "<init>",
85 "(Ljava/util/UUID;)V");
86 if (!jni_mid_ParcelUuid)
88 OIC_LOG(ERROR, TAG, "jni_mid_ParcelUuid is not available");
92 jobject jni_ParcelUuid = (*env)->NewObject(env, jni_cid_ParcelUuid, jni_mid_ParcelUuid, uuid);
95 OIC_LOG(ERROR, TAG, "Fail to get jni ParcelUuid");
99 return jni_ParcelUuid;
102 bool CALEIsBondedDevice(JNIEnv *env, jobject bluetoothDevice)
104 VERIFY_NON_NULL_RET(env, TAG, "env is null", false);
105 VERIFY_NON_NULL_RET(bluetoothDevice, TAG, "bluetoothDevice is null", false);
107 jclass jni_cid_device_list = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
108 if (!jni_cid_device_list)
110 OIC_LOG(ERROR, TAG, "jni_cid_device_list is null");
114 jmethodID jni_mid_getBondState = (*env)->GetMethodID(env, jni_cid_device_list, "getBondState",
116 if (!jni_mid_getBondState)
118 OIC_LOG(ERROR, TAG, "jni_mid_getBondState is null");
122 jint jni_bondState = (jint)(*env)->CallIntMethod(env, bluetoothDevice, jni_mid_getBondState);
124 OIC_LOG_V(DEBUG, TAG, "bond state is %d", jni_bondState);
126 if (BOND_BONDED == jni_bondState)
128 OIC_LOG(DEBUG, TAG, "remote device is bonded");
133 OIC_LOG(DEBUG, TAG, "remote device is not bonded");
140 jobjectArray CALEGetBondedDevices(JNIEnv *env)
142 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
144 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
145 if (!jni_cid_BTAdapter)
147 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_cid_BTAdapter is null");
151 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
153 METHODID_OBJECTNONPARAM);
155 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
156 jni_mid_getDefaultAdapter);
157 if (!jni_obj_BTAdapter)
159 OIC_LOG(ERROR, TAG, "getBondedDevices: bluetooth adapter is null");
163 // Get a list of currently paired devices
164 jmethodID jni_mid_getBondedDevices = (*env)->GetMethodID(env, jni_cid_BTAdapter,
166 "()Ljava/util/Set;");
167 if (!jni_mid_getBondedDevices)
169 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_mid_getBondedDevicesr is null");
173 jobject jni_obj_setPairedDevices = (*env)->CallObjectMethod(env, jni_obj_BTAdapter,
174 jni_mid_getBondedDevices);
175 if (!jni_obj_setPairedDevices)
177 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_obj_setPairedDevices is null");
181 jclass jni_cid_Set = (*env)->FindClass(env, "java/util/Set");
184 OIC_LOG(ERROR, TAG, "getBondedDevices : jni_cid_Set is null");
188 jmethodID jni_mid_toArray = (*env)->GetMethodID(env, jni_cid_Set, "toArray",
189 "()[Ljava/lang/Object;");
190 if (!jni_mid_toArray)
192 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_mid_toArray is null");
196 jobjectArray jni_arrayPairedDevices = (jobjectArray)(
197 (*env)->CallObjectMethod(env, jni_obj_setPairedDevices, jni_mid_toArray));
198 if (!jni_arrayPairedDevices)
200 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_arrayPairedDevices is null");
204 return jni_arrayPairedDevices;
207 jint CALEGetBTStateOnInfo(JNIEnv *env)
209 VERIFY_NON_NULL_RET(env, TAG, "env is null", -1);
211 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
212 if (!jni_cid_BTAdapter)
214 OIC_LOG(ERROR, TAG, "getBTStateOnInfo: jni_cid_BTAdapter is null");
218 jfieldID jni_fid_stateon = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
219 if (!jni_fid_stateon)
221 OIC_LOG(ERROR, TAG, "get_field_state is not available");
225 jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, jni_fid_stateon);
226 OIC_LOG_V(DEBUG, TAG, "bluetooth.STATE_ON state integer value : %d", jni_int_val);
231 CAResult_t CALECheckPlatformVersion(JNIEnv *env, uint16_t level)
233 jint jni_int_sdk = CALEGetBuildVersion(env);
234 if (jni_int_sdk < level)
236 OIC_LOG(ERROR, TAG, "it is not supported");
237 return CA_NOT_SUPPORTED;
243 jint CALEGetBuildVersion(JNIEnv *env)
245 VERIFY_NON_NULL_RET(env, TAG, "env is null", -1);
247 // VERSION is a nested class within android.os.Build (hence "$" rather than "/")
248 jclass jni_cls_version = (*env)->FindClass(env, "android/os/Build$VERSION");
249 if (!jni_cls_version)
251 OIC_LOG(ERROR, TAG, "jni_cls_version is null");
255 jfieldID jni_fid_sdk = (*env)->GetStaticFieldID(env, jni_cls_version, "SDK_INT", "I");
258 OIC_LOG(ERROR, TAG, "jni_fid_sdk is null");
262 jint jni_int_sdk = (*env)->GetStaticIntField(env, jni_cls_version, jni_fid_sdk);
263 OIC_LOG_V(DEBUG, TAG, "sdk version is %d", jni_int_sdk);
268 jint CALEGetBuildVersionCodeForName(JNIEnv *env, const char* versionName)
270 VERIFY_NON_NULL_RET(env, TAG, "env is null", -1);
271 VERIFY_NON_NULL_RET(versionName, TAG, "versionName is null", -1);
273 // VERSION is a nested class within android.os.Build (hence "$" rather than "/")
274 jclass jni_cls_version = (*env)->FindClass(env, "android/os/Build$VERSION_CODES");
275 if (!jni_cls_version)
277 OIC_LOG(ERROR, TAG, "jni_cls_version is null");
281 jfieldID jni_fid_version = (*env)->GetStaticFieldID(env, jni_cls_version, versionName, "I");
282 if (!jni_fid_version)
284 OIC_LOG(ERROR, TAG, "jni_fid_version is null");
288 jint jni_int_version = (*env)->GetStaticIntField(env, jni_cls_version, jni_fid_version);
289 OIC_LOG_V(DEBUG, TAG, "version [%s] is %d",versionName, jni_int_version);
291 return jni_int_version;
294 jboolean CALEIsEnableBTAdapter(JNIEnv *env)
296 VERIFY_NON_NULL_RET(env, TAG, "env is null", JNI_FALSE);
298 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
299 if (!jni_cid_BTAdapter)
301 OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter: jni_cid_BTAdapter is null");
305 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
307 METHODID_OBJECTNONPARAM);
308 if (!jni_mid_getDefaultAdapter)
310 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
314 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
315 jni_mid_getDefaultAdapter);
316 if (!jni_obj_BTAdapter)
318 OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
323 jmethodID jni_mid_isEnable = (*env)->GetMethodID(env, jni_cid_BTAdapter, "isEnabled", "()Z");
324 if (!jni_mid_isEnable)
326 OIC_LOG(ERROR, TAG, "jni_mid_isEnable is null");
330 jboolean jni_isEnable = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_isEnable);
331 OIC_LOG_V(DEBUG, TAG, "adapter state is %d", jni_isEnable);
336 jstring CALEGetAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice)
338 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
339 VERIFY_NON_NULL_RET(bluetoothDevice, TAG, "bluetoothDevice is null", NULL);
341 jclass jni_cid_device_list = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
342 if (!jni_cid_device_list)
344 OIC_LOG(ERROR, TAG, "jni_cid_device_list is null");
348 jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_device_list, "getAddress",
349 "()Ljava/lang/String;");
350 if (!jni_mid_getAddress)
352 OIC_LOG(ERROR, TAG, "jni_mid_getAddress is null");
356 jstring jni_address = (jstring)(*env)->CallObjectMethod(env, bluetoothDevice,
360 OIC_LOG(ERROR, TAG, "jni_address is null");
367 jint CALEGetConstantsValue(JNIEnv *env, const char* classType, const char* name)
369 OIC_LOG(DEBUG, TAG, "CALEGetConstantsValue");
371 VERIFY_NON_NULL_RET(env, TAG, "env", -1);
372 VERIFY_NON_NULL_RET(classType, TAG, "classType", -1);
373 VERIFY_NON_NULL_RET(name, TAG, "name", -1);
375 jclass jni_cid = (*env)->FindClass(env, classType);
378 OIC_LOG(ERROR, TAG, "jni_cid is null");
382 jfieldID jni_fieldID = (*env)->GetStaticFieldID(env, jni_cid,
386 OIC_LOG(ERROR, TAG, "jni_fieldID is null");
390 return (*env)->GetStaticIntField(env, jni_cid, jni_fieldID);