From 1b1500aed631fb995ef2479beb62a1611ccfb8d8 Mon Sep 17 00:00:00 2001 From: "jihwan.seo" Date: Thu, 22 Dec 2016 20:20:43 +0900 Subject: [PATCH] enable WITH RESPONSE and INDICATE property in android BLE since WITHOUT RESPONSE and NOTIFY property can be caused packet loss, it is better to provide WITH RESPONSE and INDICATE property. ps. Tizen BLE will be also provided INDICATE property in the near future Change-Id: Iaa802a2864511b93a676d65613567406c27ebc93 Signed-off-by: jihwan.seo Reviewed-on: https://gerrit.iotivity.org/gerrit/15913 Tested-by: jenkins-iotivity Reviewed-by: Jaehong Jo Reviewed-by: Ashok Babu Channa --- .../java/org/iotivity/ca/CaLeServerInterface.java | 5 +- .../src/bt_le_adapter/android/caleclient.c | 77 ++++++++++++++-------- .../src/bt_le_adapter/android/caleserver.c | 67 +++++++++++++++---- .../android/org_iotivity_ca_CaLeServerInterface.h | 4 +- 4 files changed, 108 insertions(+), 45 deletions(-) diff --git a/java/iotivity-android/src/main/java/org/iotivity/ca/CaLeServerInterface.java b/java/iotivity-android/src/main/java/org/iotivity/ca/CaLeServerInterface.java index 0a4bb5c..48c5b6b 100644 --- a/java/iotivity-android/src/main/java/org/iotivity/ca/CaLeServerInterface.java +++ b/java/iotivity-android/src/main/java/org/iotivity/ca/CaLeServerInterface.java @@ -62,7 +62,7 @@ public class CaLeServerInterface { BluetoothDevice device, byte[] data); private native static void caLeGattServerCharacteristicWriteRequestCallback( - BluetoothDevice device, byte[] data); + BluetoothDevice device, byte[] data, int id, int offset, byte[] value); private native static void caLeGattServerNotificationSentCallback(BluetoothDevice device, int status); @@ -108,7 +108,8 @@ public class CaLeServerInterface { super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value); - caLeGattServerCharacteristicWriteRequestCallback(device, value); + caLeGattServerCharacteristicWriteRequestCallback(device, value, requestId, + offset, value); } @Override diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c index 14c7d3d..0b299a3 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c @@ -109,6 +109,7 @@ static CALEScanState_t g_nextScanningStep = BLE_SCAN_ENABLE; static CABLEDataReceivedCallback g_CABLEClientDataReceivedCallback = NULL; +static bool g_setHighQoS = true; /** * check if retry logic for connection routine has to be stopped or not. * in case of error value including this method, connection routine has to be stopped. @@ -2625,31 +2626,39 @@ jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, j goto error_exit; } - // set Write Type - jmethodID jni_mid_setWriteType = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, - "setWriteType", "(I)V"); - if (!jni_mid_setWriteType) + if (!g_setHighQoS) { - OIC_LOG(ERROR, TAG, "jni_mid_setWriteType is null"); - goto error_exit; - } + OIC_LOG(DEBUG, TAG, "setWriteType with WRITE_TYPE_NO_RESPONSE"); + // set Write Type + jmethodID jni_mid_setWriteType = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, + "setWriteType", "(I)V"); + if (!jni_mid_setWriteType) + { + OIC_LOG(ERROR, TAG, "jni_mid_setWriteType is null"); + goto error_exit; + } - jfieldID jni_fid_no_response = (*env)->GetStaticFieldID(env, jni_cid_BTGattCharacteristic, - "WRITE_TYPE_NO_RESPONSE", "I"); - if (!jni_fid_no_response) - { - OIC_LOG(ERROR, TAG, "jni_fid_no_response is not available"); - goto error_exit; - } + jfieldID jni_fid_no_response = (*env)->GetStaticFieldID(env, jni_cid_BTGattCharacteristic, + "WRITE_TYPE_NO_RESPONSE", "I"); + if (!jni_fid_no_response) + { + OIC_LOG(ERROR, TAG, "jni_fid_no_response is not available"); + goto error_exit; + } - jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTGattCharacteristic, - jni_fid_no_response); - CACheckJNIException(env); + jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTGattCharacteristic, + jni_fid_no_response); + CACheckJNIException(env); - (*env)->CallVoidMethod(env, jni_obj_GattCharacteristic, jni_mid_setWriteType, jni_int_val); - if (CACheckJNIException(env)) + (*env)->CallVoidMethod(env, jni_obj_GattCharacteristic, jni_mid_setWriteType, jni_int_val); + if (CACheckJNIException(env)) + { + OIC_LOG(ERROR, TAG, "setWriteType has failed"); + } + } + else { - OIC_LOG(ERROR, TAG, "setWriteType has failed"); + OIC_LOG(DEBUG, TAG, "It will run with response property"); } return jni_obj_GattCharacteristic; @@ -2812,15 +2821,29 @@ CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt, goto error_exit; } - jfieldID jni_fid_NotiValue = (*env)->GetStaticFieldID(env, jni_cid_descriptor, - "ENABLE_NOTIFICATION_VALUE", "[B"); - if (!jni_fid_NotiValue) + jfieldID jni_fid_NotiValue; + if (g_setHighQoS) { - OIC_LOG(ERROR, TAG, "jni_fid_NotiValue is null"); - goto error_exit; + OIC_LOG(DEBUG, TAG, "get ENABLE_INDICATION_VALUE"); + jni_fid_NotiValue = (*env)->GetStaticFieldID(env, jni_cid_descriptor, + "ENABLE_INDICATION_VALUE", "[B"); + if (!jni_fid_NotiValue) + { + OIC_LOG(ERROR, TAG, "jni_fid_NotiValue is null"); + goto error_exit; + } + } + else + { + OIC_LOG(DEBUG, TAG, "get ENABLE_NOTIFICATION_VALUE"); + jni_fid_NotiValue = (*env)->GetStaticFieldID(env, jni_cid_descriptor, + "ENABLE_NOTIFICATION_VALUE", "[B"); + if (!jni_fid_NotiValue) + { + OIC_LOG(ERROR, TAG, "jni_fid_NotiValue is null"); + goto error_exit; + } } - - OIC_LOG(DEBUG, TAG, "get ENABLE_NOTIFICATION_VALUE"); jboolean jni_setvalue = (*env)->CallBooleanMethod( env, jni_obj_descriptor, jni_mid_setValue, diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c b/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c index 5ea9fd0..30f11a0 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c @@ -74,6 +74,8 @@ static jint g_state_disconnected = INVALID_STATE; static const char CLASSPATH_BT_ADVERTISE_CB[] = "android/bluetooth/le/AdvertiseCallback"; static const char CLASSPATH_BT_GATTSERVER[] = "android/bluetooth/BluetoothGattServer"; +static bool g_setHighQoS = true; + void CALEServerJNISetContext() { OIC_LOG(DEBUG, TAG, "CALEServerJNISetContext"); @@ -1011,22 +1013,47 @@ jobject CALEServerCreateGattService(JNIEnv *env) goto error_exit; } - jfieldID jni_fid_readProperties = (*env)->GetStaticFieldID(env, - jni_cid_bluetoothGattCharacteristic, - "PROPERTY_NOTIFY", "I"); - if (!jni_fid_readProperties) + jfieldID jni_fid_readProperties; + jfieldID jni_fid_writeProperties; + if (g_setHighQoS) { - OIC_LOG(ERROR, TAG, "jni_fid_readProperties is null"); - goto error_exit; - } + jni_fid_readProperties = (*env)->GetStaticFieldID(env, + jni_cid_bluetoothGattCharacteristic, + "PROPERTY_INDICATE", "I"); + if (!jni_fid_readProperties) + { + OIC_LOG(ERROR, TAG, "jni_fid_readProperties is null"); + goto error_exit; + } - jfieldID jni_fid_writeProperties = (*env)->GetStaticFieldID(env, - jni_cid_bluetoothGattCharacteristic, - "PROPERTY_WRITE_NO_RESPONSE", "I"); - if (!jni_fid_writeProperties) + jni_fid_writeProperties = (*env)->GetStaticFieldID(env, + jni_cid_bluetoothGattCharacteristic, + "PROPERTY_WRITE", "I"); + if (!jni_fid_writeProperties) + { + OIC_LOG(ERROR, TAG, "jni_fid_writeProperties is null"); + goto error_exit; + } + } + else { - OIC_LOG(ERROR, TAG, "jni_fid_writeProperties is null"); - goto error_exit; + jni_fid_readProperties = (*env)->GetStaticFieldID(env, + jni_cid_bluetoothGattCharacteristic, + "PROPERTY_NOTIFY", "I"); + if (!jni_fid_readProperties) + { + OIC_LOG(ERROR, TAG, "jni_fid_readProperties is null"); + goto error_exit; + } + + jni_fid_writeProperties = (*env)->GetStaticFieldID(env, + jni_cid_bluetoothGattCharacteristic, + "PROPERTY_WRITE_NO_RESPONSE", "I"); + if (!jni_fid_writeProperties) + { + OIC_LOG(ERROR, TAG, "jni_fid_writeProperties is null"); + goto error_exit; + } } jfieldID jni_fid_readPermissions = (*env)->GetStaticFieldID(env, @@ -2351,7 +2378,8 @@ Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerCharacteristicReadRequest JNIEXPORT void JNICALL Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerCharacteristicWriteRequestCallback( - JNIEnv *env, jobject obj, jobject device, jbyteArray data) + JNIEnv *env, jobject obj, jobject device, jbyteArray data, + jint requestId, jint offset, jbyteArray value) { OIC_LOG_V(DEBUG, TAG, "Gatt Server Characteristic Write Request Callback"); VERIFY_NON_NULL_VOID(env, TAG, "env"); @@ -2359,6 +2387,17 @@ Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerCharacteristicWriteReques VERIFY_NON_NULL_VOID(device, TAG, "device"); VERIFY_NON_NULL_VOID(data, TAG, "data"); + if (g_setHighQoS) + { + CALEServerSendResponse(env, device, requestId, 0, offset, value); + } + else + { + (void)requestId; + (void)offset; + (void)value; + } + // get Byte Array and covert to uint8_t* jint length = (*env)->GetArrayLength(env, data); CACheckJNIException(env); diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/org_iotivity_ca_CaLeServerInterface.h b/resource/csdk/connectivity/src/bt_le_adapter/android/org_iotivity_ca_CaLeServerInterface.h index ae066bb..b362fc9 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/org_iotivity_ca_CaLeServerInterface.h +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/org_iotivity_ca_CaLeServerInterface.h @@ -85,11 +85,11 @@ Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerCharacteristicReadRequest /* * Class: org_iotivity_ca_CaLeServerInterface * Method: caLeGattServerCharacteristicWriteRequestCallback - * Signature: (Landroid/bluetooth/BluetoothDevice;[B)V + * Signature: (Landroid/bluetooth/BluetoothDevice;[BII[B)V */ JNIEXPORT void JNICALL Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerCharacteristicWriteRequestCallback -(JNIEnv *, jobject, jobject, jbyteArray); +(JNIEnv *, jobject, jobject, jbyteArray, jint, jint, jbyteArray); /* * Class: org_iotivity_ca_CaLeServerInterface -- 2.7.4