From 9fb66e29a02ac51e17a64673473aad6e918d540f Mon Sep 17 00:00:00 2001 From: samanway-dey Date: Sat, 15 Dec 2018 15:08:10 +0530 Subject: [PATCH] Adding API for setting MTU size in Tizen BLE Client https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/366 (cherry picked from commit 8bcc7e282fd9cc55ce1ea601b289967d3f8ce681) Change-Id: I4eef40b48aa15084d2a3b66a2fea10e27b766547 Signed-off-by: samanway-dey Signed-off-by: DoHyun Pyun --- .../connectivity/src/bt_le_adapter/caleadapter.c | 2 + .../src/bt_le_adapter/tizen/caleclient_vd.c | 136 ++++++++++++++++++--- .../src/bt_le_adapter/tizen/caleutil.h | 1 + 3 files changed, 119 insertions(+), 20 deletions(-) diff --git a/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c b/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c index 6fe1343..f9d000b 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c @@ -1487,6 +1487,8 @@ static void CALEClientSendDataThread(void *threadData) return; } } +#endif +#if defined(__TIZEN__) || defined(__ANDROID__) g_mtuSize = CALEClientGetMtuSize(bleData->remoteEndpoint->addr); #endif OIC_LOG_V(INFO, CALEADAPTER_TAG, "MTU size [%d]", g_mtuSize); diff --git a/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient_vd.c b/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient_vd.c index 2e2ea1e..4d75206 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient_vd.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient_vd.c @@ -160,6 +160,16 @@ static oc_mutex g_threadWriteCharacteristicMutex = NULL; static oc_cond g_threadWriteCharacteristicCond = NULL; /** + * Mutex to synchronize the task for MTU Changed. + */ +static oc_mutex g_threadMTUChangedMutex = NULL; + +/** + * Condition for MTU Changed. + */ +static oc_cond g_threadMTUChangedCond = NULL; + +/** * Flag to check status of write characteristic. */ static bool g_isSignalSetFlag = false; @@ -212,6 +222,17 @@ void CALEGattCharacteristicChangedCb(bt_gatt_h characteristic, OIC_LOG(DEBUG, TAG, "OUT"); } +void CALEGattClientMTUChangedCb(bt_gatt_client_h client_handle, const bt_gatt_client_att_mtu_info_s *mtu_info, void *user_data) +{ + OIC_LOG(DEBUG, TAG, "IN"); + oc_mutex_lock(g_threadMTUChangedMutex); + OIC_LOG(DEBUG, TAG, "MTU changed signal"); + oc_cond_signal(g_threadMTUChangedCond); + oc_mutex_unlock(g_threadMTUChangedMutex); + OIC_LOG(DEBUG, TAG, "OUT"); +} + + void CALEGattCharacteristicWriteCb(int result, bt_gatt_h reqHandle, void *userData) { (void)reqHandle; @@ -385,11 +406,6 @@ static bool CALEIsHaveServiceImpl(bt_adapter_le_device_scan_result_info_s *scanI OIC_LOG_V(DEBUG, TAG, "Service[%s] Found in %s", uuids[i], scanInfo->remote_address); ret = true; - }else if(0 == strncasecmp(uuids[i], service_uuid,CUSTOM_UUID_LEN)) - { - OIC_LOG_V(DEBUG, TAG, "Custom Service[%s] Found in %s", - uuids[i], scanInfo->remote_address); - ret = true; } OICFree(uuids[i]); } @@ -881,6 +897,16 @@ CAResult_t CAInitGattClientMutexVariables() } } + if (NULL == g_threadMTUChangedMutex) + { + g_threadMTUChangedMutex = oc_mutex_new(); + if (NULL == g_threadMTUChangedMutex) + { + OIC_LOG(ERROR, TAG, "oc_mutex_new has failed"); + return CA_STATUS_FAILED; + } + } + if (NULL == g_startTimerCond) { g_startTimerCond = oc_cond_new(); @@ -911,6 +937,17 @@ CAResult_t CAInitGattClientMutexVariables() } } + if (NULL == g_threadMTUChangedCond) + { + g_threadMTUChangedCond = oc_cond_new(); + if (NULL == g_threadMTUChangedCond) + { + OIC_LOG(ERROR, TAG, "oc_cond_new failed"); + return CA_STATUS_FAILED; + } + } + + OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; } @@ -949,6 +986,9 @@ void CATerminateGattClientMutexVariables() oc_mutex_free(g_threadWriteCharacteristicMutex); g_threadWriteCharacteristicMutex = NULL; + oc_mutex_free(g_threadMTUChangedMutex); + g_threadMTUChangedMutex = NULL; + oc_cond_free(g_startTimerCond); g_startTimerCond = NULL; @@ -959,6 +999,9 @@ void CATerminateGattClientMutexVariables() g_threadWriteCharacteristicCond = NULL; g_isSignalSetFlag = false; + oc_cond_free(g_threadMTUChangedCond); + g_threadMTUChangedCond = NULL; + OIC_LOG(DEBUG, TAG, "OUT"); } @@ -1035,6 +1078,7 @@ void CALEGattStopDeviceScanning() return; } g_isScanningInProgress = false; + g_isUnicastScanInProgress= false; } else { @@ -1217,6 +1261,7 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress) "remote address is NULL", CA_STATUS_FAILED); LEServerInfo *serverInfo = NULL; + unsigned int mtu_size; oc_mutex_lock(g_LEServerListMutex); if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo)) { @@ -1232,9 +1277,7 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress) { OIC_LOG_V(ERROR, TAG, "bt_gatt_client_get_service Failed with ret value [%s] ", CALEGetErrorMsg(ret)); - bt_gatt_client_destroy(serverInfo->clientHandle); - CALEGattDisConnect(remoteAddress); - return CA_STATUS_FAILED; + goto error_exit; } // Server will read data on this characteristic. @@ -1246,9 +1289,7 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress) OIC_LOG_V(ERROR, TAG, "bt_gatt_service_get_characteristic Failed with ret value [%s] ", CALEGetErrorMsg(ret)); - bt_gatt_client_destroy(serverInfo->clientHandle); - CALEGattDisConnect(remoteAddress); - return CA_STATUS_FAILED; + goto error_exit; } // Server will notify data on this characteristic. @@ -1260,9 +1301,7 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress) OIC_LOG_V(ERROR, TAG, "bt_gatt_service_get_characteristic Failed with ret value [%s] ", CALEGetErrorMsg(ret)); - bt_gatt_client_destroy(serverInfo->clientHandle); - CALEGattDisConnect(remoteAddress); - return CA_STATUS_FAILED; + goto error_exit; } //TODO: This data has to be freed while unsetting the callback. @@ -1270,9 +1309,7 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress) if (NULL == addr) { OIC_LOG(ERROR, TAG, "addr is NULL"); - bt_gatt_client_destroy(serverInfo->clientHandle); - CALEGattDisConnect(remoteAddress); - return CA_STATUS_FAILED; + goto error_exit; } ret = bt_gatt_client_set_characteristic_value_changed_cb(readChrHandle, @@ -1283,15 +1320,51 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress) OIC_LOG_V(ERROR, TAG, "bt_gatt_client_set_characteristic_value_changed_cb Failed with ret value [%s]", CALEGetErrorMsg(ret)); - bt_gatt_client_destroy(serverInfo->clientHandle); - CALEGattDisConnect(remoteAddress); - return CA_STATUS_FAILED; + goto error_exit; + } + + ret = bt_gatt_client_set_att_mtu_changed_cb(serverInfo->clientHandle, + CALEGattClientMTUChangedCb, + (void *)serverInfo->remoteAddress); + if (BT_ERROR_NONE != ret) + { + OIC_LOG_V(ERROR, TAG, + "bt_gatt_client_set_att_mtu_changed_cb Failed with ret value [%s]", + CALEGetErrorMsg(ret)); + goto error_exit; } + oc_mutex_lock(g_threadMTUChangedMutex); + ret = bt_gatt_client_request_att_mtu_change(serverInfo->clientHandle, CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_MTU_HEADER_SIZE); + if (BT_ERROR_NONE != ret) + { + OIC_LOG_V(ERROR, TAG, + "bt_gatt_client_request_att_mtu_change Failed with ret value [%s]", + CALEGetErrorMsg(ret)); + oc_mutex_unlock(g_threadMTUChangedMutex); + goto error_exit; + } + + OIC_LOG(DEBUG, TAG, "wait for callback to notify MTU Changed Callback"); + oc_cond_wait(g_threadMTUChangedCond, g_threadMTUChangedMutex); + oc_mutex_unlock(g_threadMTUChangedMutex); + + OIC_LOG(DEBUG, TAG, "Done MTU"); + ret = bt_gatt_client_get_att_mtu(serverInfo->clientHandle, &mtu_size); + if (BT_ERROR_NONE != ret) + { + OIC_LOG_V(ERROR, TAG, + "bt_gatt_client_get_att_mtu Failed with ret value [%s]", + CALEGetErrorMsg(ret)); + goto error_exit; + } + OIC_LOG_V(DEBUG, TAG,"Negotiated MTU Size %d",mtu_size); + serverInfo->serviceHandle = serviceHandle; serverInfo->readChar = readChrHandle; serverInfo->writeChar = writeChrHandle; serverInfo->status = LE_STATUS_SERVICES_DISCOVERED; + serverInfo->mtu_size = mtu_size; while (serverInfo->pendingDataList) { @@ -1311,6 +1384,12 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress) OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; + +error_exit: + bt_gatt_client_destroy(serverInfo->clientHandle); + oc_mutex_unlock(g_LEServerListMutex); + CALEGattDisConnect(remoteAddress); + return CA_STATUS_FAILED; } CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress, @@ -1540,3 +1619,20 @@ exit: OIC_LOG(DEBUG, TAG, "OUT "); return CA_STATUS_OK; } + +uint16_t CALEClientGetMtuSize(const char* remote_address) +{ + LEServerInfo *serverInfo = NULL; + oc_mutex_lock(g_LEServerListMutex); + if (CA_STATUS_OK == CAGetLEServerInfo(g_LEServerList, remote_address, &serverInfo)) + { + if (serverInfo->status == LE_STATUS_SERVICES_DISCOVERED){ + OIC_LOG_V(DEBUG, TAG, "Mtu Size [%d]", serverInfo->mtu_size); + oc_mutex_unlock(g_LEServerListMutex); + return serverInfo->mtu_size; + } + } + oc_mutex_unlock(g_LEServerListMutex); + return CA_DEFAULT_BLE_MTU_SIZE; + +} diff --git a/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleutil.h b/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleutil.h index fbfc7db..e25af92 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleutil.h +++ b/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleutil.h @@ -67,6 +67,7 @@ typedef struct bt_gatt_h writeChar; char *remoteAddress; LEDataList *pendingDataList; + uint16_t mtu_size; LEDeviceStatus status; } LEServerInfo; -- 2.7.4