From 02842b31e3adc1d8cc02bf74c4b3925d64b4cb81 Mon Sep 17 00:00:00 2001 From: Jaewook Jung Date: Mon, 2 May 2016 16:43:06 +0900 Subject: [PATCH] change the condition for BLE connected callback As BLE connected callback means communication on BLE is available for user, the callback should be called on DescriptorWrite success callback which is the last step for BLE communication. However, since there is a case that gatt descriptor can not be found, the callback needs to be called on ServicesDiscovered callback in this case. Change-Id: I9d0daa233a1b605e70857d9fcc584f9224f53e03 Signed-off-by: Jaewook Jung Reviewed-on: https://gerrit.iotivity.org/gerrit/8025 Tested-by: jenkins-iotivity Reviewed-by: Jon A. Cruz --- .../org/iotivity/ca/CaLeClientInterface.java | 3 + .../src/bt_le_adapter/android/caleclient.c | 49 +++++-- .../src/bt_le_adapter/android/caleclient.h | 1 + .../src/bt_le_adapter/android/calenwmonitor.c | 124 ++++++++++++------ .../src/bt_le_adapter/android/caleutils.h | 1 + 5 files changed, 126 insertions(+), 52 deletions(-) diff --git a/android/android_api/base/src/main/java/org/iotivity/ca/CaLeClientInterface.java b/android/android_api/base/src/main/java/org/iotivity/ca/CaLeClientInterface.java index 4f37d9a46..6fc82942d 100644 --- a/android/android_api/base/src/main/java/org/iotivity/ca/CaLeClientInterface.java +++ b/android/android_api/base/src/main/java/org/iotivity/ca/CaLeClientInterface.java @@ -94,6 +94,8 @@ public class CaLeClientInterface { private native static void caLeGattNWServicesDiscoveredCallback(BluetoothGatt gatt, int status); + private native static void caLeGattNWDescriptorWriteCallback(BluetoothGatt gatt, int status); + private native static void caLeGattCharacteristicWriteCallback( BluetoothGatt gatt, byte[] data, int status); @@ -251,6 +253,7 @@ public class CaLeClientInterface { super.onDescriptorWrite(gatt, descriptor, status); caLeGattDescriptorWriteCallback(gatt, status); + caLeGattNWDescriptorWriteCallback(gatt, status); } @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 14e8fdb71..5773e6ce0 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c @@ -1537,6 +1537,9 @@ CAResult_t CALEClientSetFlagToState(JNIEnv *env, jstring jni_address, jint state case CA_LE_AUTO_CONNECT_FLAG: curState->autoConnectFlag = flag; break; + case CA_LE_DESCRIPTOR_FOUND: + curState->isDescriptorFound = flag; + break; default: break; } @@ -1579,6 +1582,9 @@ jboolean CALEClientGetFlagFromState(JNIEnv *env, jstring jni_address, jint state case CA_LE_AUTO_CONNECT_FLAG: ret = curState->autoConnectFlag; break; + case CA_LE_DESCRIPTOR_FOUND: + ret = curState->isDescriptorFound; + break; default: break; } @@ -4183,12 +4189,12 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattServicesDiscoveredCallback(JNIE jobject gatt, jint status) { - OIC_LOG_V(INFO, TAG, "CALeGattServicesDiscoveredCallback - status %d: ", status); + OIC_LOG_V(INFO, TAG, "CALeGattServicesDiscoveredCallback - status %d", status); VERIFY_NON_NULL_VOID(env, TAG, "env is null"); VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null"); - if (0 != status) // discovery error + if (GATT_SUCCESS != status) // discovery error { CALEClientSendFinish(env, gatt); return; @@ -4235,6 +4241,13 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattServicesDiscoveredCallback(JNIE { OIC_LOG_V(INFO, TAG, "Descriptor is not found : %d", res); + res = CALEClientSetFlagToState(env, jni_address, CA_LE_DESCRIPTOR_FOUND, JNI_FALSE); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "CALEClientSetFlagToState has failed"); + goto error_exit; + } + res = CALEClientUpdateDeviceState(address, CA_LE_CONNECTION_STATE, STATE_SERVICE_CONNECTED); if (CA_STATUS_OK != res) @@ -4253,6 +4266,15 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattServicesDiscoveredCallback(JNIE } } } + else + { + res = CALEClientSetFlagToState(env, jni_address, CA_LE_DESCRIPTOR_FOUND, JNI_TRUE); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "CALEClientSetFlagToState has failed"); + goto error_exit; + } + } OIC_LOG(INFO, TAG, "ServicesDiscovery is successful"); (*env)->ReleaseStringUTFChars(env, jni_address, address); @@ -4320,7 +4342,7 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicWriteCallback( g_clientErrorCallback(address, data, length, CA_SEND_FAILED); } - CALEClientSendFinish(env, gatt); + (*env)->ReleaseStringUTFChars(env, jni_address, address); goto error_exit; } } @@ -4424,7 +4446,7 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattDescriptorWriteCallback(JNIEnv jobject gatt, jint status) { - OIC_LOG_V(INFO, TAG, "CALeGattDescriptorWriteCallback - status %d: ", status); + OIC_LOG_V(INFO, TAG, "CALeGattDescriptorWriteCallback - status %d", status); VERIFY_NON_NULL_VOID(env, TAG, "env is null"); VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null"); @@ -4441,18 +4463,19 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattDescriptorWriteCallback(JNIEnv } const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL); - if (address) + if (!address) { - CAResult_t res = CALEClientUpdateDeviceState(address, CA_LE_CONNECTION_STATE, - STATE_SERVICE_CONNECTED); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed"); - (*env)->ReleaseStringUTFChars(env, jni_address, address); - goto error_exit; - } + goto error_exit; } + + CAResult_t res = CALEClientUpdateDeviceState(address, CA_LE_CONNECTION_STATE, + STATE_SERVICE_CONNECTED); (*env)->ReleaseStringUTFChars(env, jni_address, address); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed"); + goto error_exit; + } if (g_sendBuffer) { diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.h b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.h index 1f5f7bba3..df49c448d 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.h +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.h @@ -48,6 +48,7 @@ typedef struct le_state_info uint16_t connectedState; uint16_t sendState; jboolean autoConnectFlag; + jboolean isDescriptorFound; } CALEState_t; /** diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/calenwmonitor.c b/resource/csdk/connectivity/src/bt_le_adapter/android/calenwmonitor.c index 579fa8a3d..cf75d871a 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/calenwmonitor.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/calenwmonitor.c @@ -262,6 +262,33 @@ CAResult_t CAUnsetLENWConnectionStateChangedCb() return CA_STATUS_OK; } +static CAResult_t CALEStateConnectedCallback(JNIEnv *env, jstring jni_address, + jboolean isDescriptorFound) +{ + VERIFY_NON_NULL(env, TAG, "env"); + VERIFY_NON_NULL(jni_address, TAG, "jni_address"); + + if (CALEClientGetFlagFromState(env, jni_address, CA_LE_DESCRIPTOR_FOUND) == isDescriptorFound) + { + if (g_bleConnectionStateChangedCallback) + { + const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL); + if (!address) + { + OIC_LOG(ERROR, TAG, "address is null"); + return CA_STATUS_FAILED; + } + + g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, true); + OIC_LOG(DEBUG, TAG, "BLE is connected"); + + (*env)->ReleaseStringUTFChars(env, jni_address, address); + } + } + + return CA_STATUS_OK; +} + JNIEXPORT void JNICALL Java_org_iotivity_ca_CaLeClientInterface_caLeStateChangedCallback(JNIEnv *env, jobject obj, jint status) @@ -370,12 +397,11 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattNWConnectionStateChangeCallback jint status, jint newState) { - VERIFY_NON_NULL_VOID(env, TAG, "env is null"); - VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); - VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null"); - OIC_LOG_V(DEBUG, TAG, "CALeGattNWConnectionStateChangeCallback - status %d, newstate %d", status, newState); + VERIFY_NON_NULL_VOID(env, TAG, "env"); + VERIFY_NON_NULL_VOID(obj, TAG, "obj"); + VERIFY_NON_NULL_VOID(gatt, TAG, "gatt"); jint state_disconnected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE, "STATE_DISCONNECTED"); @@ -412,39 +438,38 @@ JNIEXPORT void JNICALL Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerNWConnectionStateChangeCallback( JNIEnv *env, jobject obj, jobject device, jint status, jint newState) { - OIC_LOG(DEBUG, TAG, " Gatt Server NWConnectionStateChange Callback"); - + OIC_LOG_V(DEBUG, TAG, "caLeGattServerNWConnectionStateChangeCallback - status %d, newstate %d", + status, newState); VERIFY_NON_NULL_VOID(env, TAG, "env"); VERIFY_NON_NULL_VOID(obj, TAG, "obj"); VERIFY_NON_NULL_VOID(device, TAG, "device"); - (void)status; - jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device); - if (!jni_remoteAddress) + if (CONNECTION_FAILED_TO_BE_EASTABLISHED != status) { - OIC_LOG(ERROR, TAG, "jni_remoteAddress is null"); - return; - } + if (g_bleConnectionStateChangedCallback) + { + jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device); + if (!jni_remoteAddress) + { + OIC_LOG(ERROR, TAG, "jni_remoteAddress is null"); + return; + } - const char* address = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL); - if (!address) - { - OIC_LOG(ERROR, TAG, "address is null"); - return; - } + const char* address = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL); + if (!address) + { + OIC_LOG(ERROR, TAG, "address is null"); + return; + } - // STATE_DISCONNECTED - jint state_disconnected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE, - "STATE_DISCONNECTED"); + // STATE_DISCONNECTED + jint state_disconnected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE, + "STATE_DISCONNECTED"); - // STATE_CONNECTED - jint state_connected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE, - "STATE_CONNECTED"); + // STATE_CONNECTED + jint state_connected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE, + "STATE_CONNECTED"); - if (CONNECTION_FAILED_TO_BE_EASTABLISHED != status) - { - if (g_bleConnectionStateChangedCallback) - { if (state_disconnected == newState) { OIC_LOG_V(DEBUG, TAG, "LE Disconnected state is %d, %s", newState, address); @@ -459,9 +484,10 @@ Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerNWConnectionStateChangeCa { OIC_LOG_V(DEBUG, TAG, "Unknown state : %d, %s", newState, address); } + + (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, address); } } - (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, address); } JNIEXPORT void JNICALL @@ -470,7 +496,7 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattNWServicesDiscoveredCallback(JN jobject gatt, jint status) { - OIC_LOG_V(DEBUG, TAG, "caLeGattNWServicesDiscoveredCallback - status %d: ", status); + OIC_LOG_V(DEBUG, TAG, "caLeGattNWServicesDiscoveredCallback - status %d", status); VERIFY_NON_NULL_VOID(env, TAG, "env"); VERIFY_NON_NULL_VOID(obj, TAG, "obj"); VERIFY_NON_NULL_VOID(gatt, TAG, "gatt"); @@ -480,24 +506,44 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattNWServicesDiscoveredCallback(JN jstring jni_address = CALEGetAddressFromGatt(env, gatt); if (!jni_address) { - OIC_LOG(ERROR, TAG, "CALEGetAddressFromGatt is null"); + OIC_LOG(ERROR, TAG, "jni_address is null"); return; } - const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL); - if (!address) + if (CA_STATUS_OK != CALEStateConnectedCallback(env, jni_address, JNI_FALSE)) { - OIC_LOG(ERROR, TAG, "address is null"); - (*env)->DeleteLocalRef(env, jni_address); + OIC_LOG(ERROR, TAG, "CALEStateConnectedCallback has failed"); + } + + (*env)->DeleteLocalRef(env, jni_address); + } +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ca_CaLeClientInterface_caLeGattNWDescriptorWriteCallback(JNIEnv *env, + jobject obj, + jobject gatt, + jint status) +{ + OIC_LOG_V(DEBUG, TAG, "caLeGattNWDescriptorWriteCallback - status %d", status); + VERIFY_NON_NULL_VOID(env, TAG, "env"); + VERIFY_NON_NULL_VOID(obj, TAG, "obj"); + VERIFY_NON_NULL_VOID(gatt, TAG, "gatt"); + + if (GATT_SUCCESS == status) + { + jstring jni_address = CALEGetAddressFromGatt(env, gatt); + if (!jni_address) + { + OIC_LOG(ERROR, TAG, "jni_address is null"); return; } - if (g_bleConnectionStateChangedCallback) + if (CA_STATUS_OK != CALEStateConnectedCallback(env, jni_address, JNI_TRUE)) { - g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, true); - OIC_LOG(DEBUG, TAG, "BLE is connected"); + OIC_LOG(ERROR, TAG, "CALEStateConnectedCallback has failed"); } - (*env)->ReleaseStringUTFChars(env, jni_address, address); + (*env)->DeleteLocalRef(env, jni_address); } } diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h b/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h index 478b264a9..dd014685b 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h @@ -39,6 +39,7 @@ extern "C" #define CA_LE_AUTO_CONNECT_FLAG 1 #define CA_LE_CONNECTION_STATE 2 #define CA_LE_SEND_STATE 3 +#define CA_LE_DESCRIPTOR_FOUND 4 /* Service UUID */ static const char OIC_GATT_SERVICE_UUID[] = CA_GATT_SERVICE_UUID; -- 2.34.1