From 4bbdfcb15b193b866ae1ce227b1584f968b4899e Mon Sep 17 00:00:00 2001 From: "jihwan.seo" Date: Thu, 5 Jan 2017 12:27:17 +0900 Subject: [PATCH] [IOT-1731] stop BLE advertising for disconnection status.(android/tizen) since advertising can be caused a big problem related battery. it should be stopped when it is not used. Change-Id: Ib645dafe9cba3218972b655cfa0f5b067ad971eb Signed-off-by: jihwan.seo Reviewed-on: https://gerrit.iotivity.org/gerrit/16131 Tested-by: jenkins-iotivity Reviewed-by: Jaehong Jo Reviewed-by: Dan Mihai Reviewed-by: Ashok Babu Channa --- .../src/bt_le_adapter/android/caleserver.c | 90 +++++++++++++--------- .../src/bt_le_adapter/android/caleserver.h | 13 +++- .../src/bt_le_adapter/tizen/caleserver.c | 19 ++++- 3 files changed, 81 insertions(+), 41 deletions(-) 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 30f11a0..fe4f2a7 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c @@ -411,7 +411,7 @@ CAResult_t CALEServerSendResponse(JNIEnv *env, jobject device, jint requestId, j return CA_STATUS_OK; } -CAResult_t CALEStartAdvertise() +CAResult_t CALEServerStartAdvertise() { if (!g_jvm) { @@ -436,10 +436,10 @@ CAResult_t CALEStartAdvertise() } // start advertise - CAResult_t ret = CALEServerStartAdvertise(env, g_leAdvertiseCallback); + CAResult_t ret = CALEServerStartAdvertiseImpl(env, g_leAdvertiseCallback); if (CA_STATUS_OK != ret) { - OIC_LOG(ERROR, TAG, "CALEServerStartAdvertise has failed"); + OIC_LOG(ERROR, TAG, "CALEServerStartAdvertiseImpl has failed"); } if (isAttached) @@ -449,9 +449,9 @@ CAResult_t CALEStartAdvertise() return ret; } -CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) +CAResult_t CALEServerStartAdvertiseImpl(JNIEnv *env, jobject advertiseCallback) { - OIC_LOG(DEBUG, TAG, "IN - CALEServerStartAdvertise"); + OIC_LOG(DEBUG, TAG, "IN - CALEServerStartAdvertiseImpl"); VERIFY_NON_NULL(env, TAG, "env is null"); VERIFY_NON_NULL(advertiseCallback, TAG, "advertiseCallback is null"); @@ -752,9 +752,47 @@ error_exit: return CA_STATUS_FAILED; } -CAResult_t CALEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback) +CAResult_t CALEServerStopAdvertise() { - OIC_LOG(DEBUG, TAG, "LEServerStopAdvertise"); + if (!g_jvm) + { + OIC_LOG(ERROR, TAG, "g_jvm is null"); + return CA_STATUS_FAILED; + } + + bool isAttached = false; + JNIEnv* env = NULL; + jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); + if (JNI_OK != res) + { + OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer"); + res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); + + if (JNI_OK != res) + { + OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); + return CA_STATUS_FAILED; + } + isAttached = true; + } + + CAResult_t ret = CALEServerStopAdvertiseImpl(env, g_leAdvertiseCallback); + if (CA_STATUS_OK != ret) + { + OIC_LOG(ERROR, TAG, "CALEServerStopAdvertise has failed"); + } + + if (isAttached) + { + (*g_jvm)->DetachCurrentThread(g_jvm); + } + + return ret; +} + +CAResult_t CALEServerStopAdvertiseImpl(JNIEnv *env, jobject advertiseCallback) +{ + OIC_LOG(DEBUG, TAG, "CALEServerStopAdvertiseImpl"); VERIFY_NON_NULL(env, TAG, "env is null"); VERIFY_NON_NULL(advertiseCallback, TAG, "advertiseCallback is null"); @@ -1789,29 +1827,7 @@ CAResult_t CALEServerStopMulticastServer() return CA_STATUS_FAILED; } - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); - return CA_STATUS_FAILED; - } - - bool isAttached = false; - JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer"); - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return CA_STATUS_FAILED; - } - isAttached = true; - } - - CAResult_t ret = CALEServerStopAdvertise(env, g_leAdvertiseCallback); + CAResult_t ret = CALEServerStopAdvertise(); if (CA_STATUS_OK != ret) { OIC_LOG(ERROR, TAG, "CALEServerStopAdvertise has failed"); @@ -1819,11 +1835,6 @@ CAResult_t CALEServerStopMulticastServer() g_isStartServer = false; - if (isAttached) - { - (*g_jvm)->DetachCurrentThread(g_jvm); - } - OIC_LOG(DEBUG, TAG, "OUT - CALEServerStopMulticastServer"); return ret; } @@ -2320,6 +2331,13 @@ Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerConnectionStateChangeCall OIC_LOG(DEBUG, TAG, "add connected device to cache"); CALEServerAddDeviceToList(env, device); } + + CAResult_t res = CALEServerStopAdvertise(); + if (CA_STATUS_OK != res) + { + OIC_LOG(DEBUG, TAG, "CALEServerStopAdvertise has failed"); + } + (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress); } else if (newState == g_state_disconnected) @@ -2334,7 +2352,7 @@ Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerConnectionStateChangeCall } // start advertise - ret = CALEServerStartAdvertise(env, g_leAdvertiseCallback); + ret = CALEServerStartAdvertise(); if (CA_STATUS_OK != ret) { OIC_LOG(ERROR, TAG, "CALEServerStartAdvertise has failed"); diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.h b/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.h index 83943b6..2854df8 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.h +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.h @@ -128,8 +128,9 @@ CAResult_t CALEServerCreateJniInterfaceObject(); /** * start advertise in gatt server. + * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h). */ -CAResult_t CALEStartAdvertise(); +CAResult_t CALEServerStartAdvertise(); /** * start advertise in gatt server. @@ -138,7 +139,13 @@ CAResult_t CALEStartAdvertise(); * advertisement result. * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h). */ -CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback); +CAResult_t CALEServerStartAdvertiseImpl(JNIEnv *env, jobject advertiseCallback); + +/** + * stop advertise in gatt server. + * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h). + */ +CAResult_t CALEServerStopAdvertise(); /** * stop advertise in gatt server. @@ -147,7 +154,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback); * advertisement result. * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h). */ -CAResult_t CALEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback); +CAResult_t CALEServerStopAdvertiseImpl(JNIEnv *env, jobject advertiseCallback); /** * open a gatt server. diff --git a/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleserver.c b/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleserver.c index fdf2e0a..6e72ed5 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleserver.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleserver.c @@ -129,13 +129,14 @@ void CALEGattServerConnectionStateChanged(bool connected, const char *remoteAddr { VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address"); + CAResult_t res = CA_STATUS_OK; if (connected) { OIC_LOG_V(DEBUG, TAG, "Connected to [%s]", remoteAddress); char *addr = OICStrdup(remoteAddress); oc_mutex_lock(g_LEClientListMutex); - CAResult_t result = CAAddLEClientInfoToList(&g_LEClientList, addr); - if (CA_STATUS_OK != result) + res = CAAddLEClientInfoToList(&g_LEClientList, addr); + if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "CAAddLEClientInfoToList failed"); oc_mutex_unlock(g_LEClientListMutex); @@ -143,6 +144,13 @@ void CALEGattServerConnectionStateChanged(bool connected, const char *remoteAddr return; } oc_mutex_unlock(g_LEClientListMutex); + + res = CALEStopAdvertise(); + if (CA_STATUS_OK != res) + { + OIC_LOG_V(ERROR, TAG, "Failed to stop advertising [%d]", res); + return; + } } else { @@ -150,6 +158,13 @@ void CALEGattServerConnectionStateChanged(bool connected, const char *remoteAddr oc_mutex_lock(g_LEClientListMutex); CARemoveLEClientInfoFromList(&g_LEClientList, remoteAddress); oc_mutex_unlock(g_LEClientListMutex); + + res = CALEStartAdvertise(CA_GATT_SERVICE_UUID); + if (CA_STATUS_OK != res) + { + OIC_LOG_V(ERROR, TAG, "Failed to start advertising [%d]", res); + return; + } } } -- 2.7.4