From: Amit KS Date: Thu, 10 Jan 2019 06:31:45 +0000 (+0530) Subject: Added scanning of custom UUID's and MTU Negotiation Logic X-Git-Tag: accepted/tizen/5.0/unified/20190111.055256~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2fb99f1ecc25ff391beb88f7e0c2dc5d77a95269;p=platform%2Fupstream%2Fiotivity.git Added scanning of custom UUID's and MTU Negotiation Logic -VD Devices advertise custom UUID's So added handling of it in scan callback. -Added MTU negotiated to be 512 https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/357 (cherry picked from commit 2a0f1e9134fe15d002786a92919b69d2a0b852e9) Change-Id: I1f7e31b16c86701f08a56d693565f73eaab19fac Signed-off-by: agrkush Signed-off-by: Amit KS --- diff --git a/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient.c b/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient.c index 6884a8e..ad5765a 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient.c @@ -46,6 +46,13 @@ #define MICROSECS_PER_SEC 1000000 #define WAIT_TIME_WRITE_CHARACTERISTIC 10 * MICROSECS_PER_SEC +//For custom uuid ble server +#define CA_GATT_CUSTOM_UUID "4209" +#define CA_GATT_CUSTOM_UUID2 "4204" +#define CUSTOM_UUID_LEN 4 + +static const int samsung_code = 117; + uint64_t const TIMEOUT = 30 * MICROSECS_PER_SEC; /** @@ -153,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; @@ -205,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; @@ -305,18 +333,6 @@ void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress) g_isConnectionInProgress = false; oc_mutex_unlock(g_isConnectionInProgressMutex); - // Resume the scanning - oc_mutex_lock(g_scanMutex); - if (g_isMulticastInProgress || g_isUnicastScanInProgress) - { - CAResult_t ret = CALEGattStartDeviceScanning(); - if (CA_STATUS_OK != ret) - { - OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning Failed"); - } - } - oc_mutex_unlock(g_scanMutex); - LEServerInfo *serverInfo = NULL; oc_mutex_lock(g_LEServerListMutex); if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo)) @@ -382,6 +398,33 @@ static bool CALEIsHaveServiceImpl(bt_adapter_le_device_scan_result_info_s *scanI } OICFree(uuids); } + + if(ret == false){ + char *man_data = NULL; + int man_data_len; + int man_id; + result = bt_adapter_le_get_scan_result_manufacturer_data(scanInfo, + pkt_type, &man_id, &man_data, &man_data_len); + + if (result == BT_ERROR_NONE && NULL != man_data) + { + char *compare_man_data = OICMalloc((man_data_len*2)+1); + int pos =0; + for(int i=0;iremote_address); + ret = true; + } + OICFree(compare_man_data); + OICFree(man_data); + } + } return ret; } @@ -425,70 +468,19 @@ void CALEAdapterScanResultCb(int result, bt_adapter_le_device_scan_result_info_s } oc_mutex_unlock(g_isScanningInProgressMutex); - oc_mutex_lock(g_scanMutex); - bool isMulticastInProgress = g_isMulticastInProgress; - oc_mutex_unlock(g_scanMutex); - - LEServerInfo *serverInfo = NULL; - oc_mutex_lock(g_LEServerListMutex); - CAResult_t ret = CAGetLEServerInfo(g_LEServerList, scanInfo->remote_address, &serverInfo); - - if (CA_STATUS_OK == ret && serverInfo->status == LE_STATUS_UNICAST_PENDING) + if (CALEIsHaveService(scanInfo, CA_GATT_SERVICE_UUID) || + CALEIsHaveService(scanInfo, CA_GATT_CUSTOM_UUID)|| + CALEIsHaveService(scanInfo, CA_GATT_CUSTOM_UUID2)) { - // Stop the scan if no other device is in unicast pending - // state and if multicast is not in progress - LEServerInfoList *curNode = g_LEServerList; - for (; curNode; curNode = curNode->next) - { - if (curNode->serverInfo == serverInfo) - { - continue; - } - if (curNode->serverInfo->status == LE_STATUS_UNICAST_PENDING) - { - break; - } - } + OIC_LOG_V(DEBUG, TAG, "Device [%s] supports OIC or custom service", scanInfo->remote_address); - if (NULL == curNode) - { - oc_mutex_lock(g_scanMutex); - if (!g_isMulticastInProgress && g_isUnicastScanInProgress) - { - CALEGattStopDeviceScanning(); - g_isUnicastScanInProgress = false; - oc_cond_signal(g_scanningTimeCond); - } - oc_mutex_unlock(g_scanMutex); - } - - if (!CALEIsHaveService(scanInfo, CA_GATT_SERVICE_UUID)) - { - serverInfo->status = LE_STATUS_INVALID; - OIC_LOG_V(DEBUG, TAG, "Device [%s] does not support OIC service", serverInfo->remoteAddress); - oc_mutex_unlock(g_LEServerListMutex); - return; - } - - serverInfo->status = LE_STATUS_CONNECTION_INITIATED; - if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress)) - { - serverInfo->status = LE_STATUS_DISCOVERED; - OIC_LOG_V(ERROR, TAG, "Could not initiate connection to [%s]", serverInfo->remoteAddress); - oc_mutex_unlock(g_LEServerListMutex); - return; - } - oc_mutex_unlock(g_LEServerListMutex); - OIC_LOG(DEBUG, TAG, "OUT"); - return; - } - - if (isMulticastInProgress) - { + LEServerInfo *serverInfo = NULL; + oc_mutex_lock(g_LEServerListMutex); + CAResult_t ret = CAGetLEServerInfo(g_LEServerList, scanInfo->remote_address, &serverInfo); if (CA_STATUS_OK != ret) { OIC_LOG_V(DEBUG, TAG, - "Newly discovered device with address [%s], adding to list", scanInfo->remote_address); + "Newly discovered device with address [%s] ", scanInfo->remote_address); char *addr = OICStrdup(scanInfo->remote_address); if (NULL == addr) @@ -515,49 +507,34 @@ void CALEAdapterScanResultCb(int result, bt_adapter_le_device_scan_result_info_s CAFreeLEServerInfo(serverInfo); return; } + }else { + OIC_LOG_V(DEBUG, TAG, + "Device Present with address [%s] ", scanInfo->remote_address); - if (!CALEIsHaveService(scanInfo, CA_GATT_SERVICE_UUID)) - { - serverInfo->status = LE_STATUS_INVALID; - OIC_LOG_V(DEBUG, TAG, "Device [%s] does not support OIC service", serverInfo->remoteAddress); - oc_mutex_unlock(g_LEServerListMutex); - return; - } - - oc_mutex_lock(g_multicastDataListMutex); - uint32_t lengthData = u_arraylist_length(g_multicastDataList); - for (uint32_t len = 0; len < lengthData; ++len) - { - LEData *multicastData = (LEData *)u_arraylist_get(g_multicastDataList, len); - if (NULL == multicastData) - { - OIC_LOG(ERROR, TAG, "multicastData is NULL"); - continue; - } - if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, - multicastData->data, multicastData->dataLength)) + if(serverInfo->status == LE_STATUS_UNICAST_PENDING){ + bt_gatt_client_h clientHandle = NULL; + int32_t ret = bt_gatt_client_create(serverInfo->remoteAddress, &clientHandle); + if (BT_ERROR_NONE != ret || NULL == clientHandle) { - OIC_LOG(ERROR, TAG, "Failed to add to pending list"); - continue; + OIC_LOG_V(ERROR, TAG, + "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret)); + CALEGattDisConnect(serverInfo->remoteAddress); + oc_mutex_unlock(g_LEServerListMutex); + return ; } - } - oc_mutex_unlock(g_multicastDataListMutex); - } + serverInfo->clientHandle = clientHandle; - if (serverInfo->status == LE_STATUS_DISCOVERED) - { - // Initiate connection if not yet initiated - serverInfo->status = LE_STATUS_CONNECTION_INITIATED; - if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress)) - { - OIC_LOG_V(ERROR, TAG, "Could not initiate connection to [%s]", serverInfo->remoteAddress); - serverInfo->status = LE_STATUS_DISCOVERED; - } - } - else - { - OIC_LOG_V(DEBUG, TAG, "Device already discovered, status= [%d]", serverInfo->status); - } + serverInfo->status = LE_STATUS_CONNECTION_INITIATED; + if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress)) + { + OIC_LOG_V(ERROR, TAG, "Could not initiate connection to [%s]", serverInfo->remoteAddress); + serverInfo->status = LE_STATUS_DISCOVERED; + CADestroyLEDataList(&serverInfo->pendingDataList); + oc_mutex_unlock(g_LEServerListMutex); + return ; + } + } + } } oc_mutex_unlock(g_LEServerListMutex); @@ -872,6 +849,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(); @@ -902,6 +889,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; } @@ -940,6 +938,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; @@ -950,6 +951,9 @@ void CATerminateGattClientMutexVariables() g_threadWriteCharacteristicCond = NULL; g_isSignalSetFlag = false; + oc_cond_free(g_threadMTUChangedCond); + g_threadMTUChangedCond = NULL; + OIC_LOG(DEBUG, TAG, "OUT"); } @@ -1026,6 +1030,7 @@ void CALEGattStopDeviceScanning() return; } g_isScanningInProgress = false; + g_isUnicastScanInProgress= false; } else { @@ -1207,25 +1212,24 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress) VERIFY_NON_NULL_RET(remoteAddress, TAG, "remote address is NULL", CA_STATUS_FAILED); - bt_gatt_client_h clientHandle = NULL; - int32_t ret = bt_gatt_client_create(remoteAddress, &clientHandle); - if (BT_ERROR_NONE != ret || NULL == clientHandle) + LEServerInfo *serverInfo = NULL; + unsigned int mtu_size; + oc_mutex_lock(g_LEServerListMutex); + if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo)) { - OIC_LOG_V(ERROR, TAG, - "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret)); + oc_mutex_unlock(g_LEServerListMutex); + OIC_LOG_V(ERROR, TAG, "Could not get server info for [%s]", remoteAddress); CALEGattDisConnect(remoteAddress); return CA_STATUS_FAILED; } bt_gatt_h serviceHandle = NULL; - ret = bt_gatt_client_get_service(clientHandle, CA_GATT_SERVICE_UUID, &serviceHandle); + int32_t ret = bt_gatt_client_get_service(serverInfo->clientHandle, CA_GATT_SERVICE_UUID, &serviceHandle); if (BT_ERROR_NONE != ret || NULL == serviceHandle) { OIC_LOG_V(ERROR, TAG, "bt_gatt_client_get_service Failed with ret value [%s] ", CALEGetErrorMsg(ret)); - bt_gatt_client_destroy(clientHandle); - CALEGattDisConnect(remoteAddress); - return CA_STATUS_FAILED; + goto error_exit; } // Server will read data on this characteristic. @@ -1237,9 +1241,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(clientHandle); - CALEGattDisConnect(remoteAddress); - return CA_STATUS_FAILED; + goto error_exit; } // Server will notify data on this characteristic. @@ -1251,9 +1253,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(clientHandle); - CALEGattDisConnect(remoteAddress); - return CA_STATUS_FAILED; + goto error_exit; } @@ -1262,9 +1262,7 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress) if (NULL == addr) { OIC_LOG(ERROR, TAG, "addr is NULL"); - bt_gatt_client_destroy(clientHandle); - CALEGattDisConnect(remoteAddress); - return CA_STATUS_FAILED; + goto error_exit; } ret = bt_gatt_client_set_characteristic_value_changed_cb(readChrHandle, @@ -1275,27 +1273,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(clientHandle); - CALEGattDisConnect(remoteAddress); - return CA_STATUS_FAILED; + goto error_exit; } - LEServerInfo *serverInfo = NULL; - oc_mutex_lock(g_LEServerListMutex); - if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo)) + ret = bt_gatt_client_set_att_mtu_changed_cb(serverInfo->clientHandle, + CALEGattClientMTUChangedCb, + (void *)serverInfo->remoteAddress); + if (BT_ERROR_NONE != ret) { - oc_mutex_unlock(g_LEServerListMutex); - OIC_LOG_V(ERROR, TAG, "Could not get server info for [%s]", remoteAddress); - bt_gatt_client_destroy(clientHandle); - CALEGattDisConnect(remoteAddress); - return CA_STATUS_FAILED; + 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->clientHandle = clientHandle; serverInfo->serviceHandle = serviceHandle; serverInfo->readChar = readChrHandle; serverInfo->writeChar = writeChrHandle; serverInfo->status = LE_STATUS_SERVICES_DISCOVERED; + serverInfo->mtu_size = mtu_size; while (serverInfo->pendingDataList) { @@ -1315,6 +1337,13 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress) OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; + +error_exit: + bt_gatt_client_destroy(serverInfo->clientHandle); + serverInfo->clientHandle = NULL; + oc_mutex_unlock(g_LEServerListMutex); + CALEGattDisConnect(remoteAddress); + return CA_STATUS_FAILED; } CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress, @@ -1364,7 +1393,7 @@ CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress, if (CA_STATUS_OK != CAAddLEServerInfoToList(&g_LEServerList, serverInfo)) { oc_mutex_unlock(g_LEServerListMutex); - OIC_LOG_V(ERROR, TAG, "Could not add [%s] to server list", serverInfo->remoteAddress); + OIC_LOG_V(ERROR, TAG, "Could not add [%s] to server list", remoteAddress); CAFreeLEServerInfo(serverInfo); return CA_STATUS_FAILED; } @@ -1376,8 +1405,6 @@ CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress, return CA_STATUS_FAILED; } - oc_mutex_unlock(g_LEServerListMutex); - oc_mutex_lock(g_scanMutex); if (!g_isMulticastInProgress && !g_isUnicastScanInProgress) { @@ -1400,8 +1427,6 @@ CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress, } oc_mutex_unlock(g_scanMutex); - OIC_LOG(DEBUG, TAG, "OUT"); - return CA_STATUS_OK; } if (serverInfo->status == LE_STATUS_DISCOVERED) @@ -1413,6 +1438,17 @@ CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress, return CA_STATUS_FAILED; } + bt_gatt_client_h clientHandle = NULL; + int32_t ret = bt_gatt_client_create(serverInfo->remoteAddress, &clientHandle); + if (BT_ERROR_NONE != ret || NULL == clientHandle) + { + OIC_LOG_V(ERROR, TAG, + "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret)); + CALEGattDisConnect(serverInfo->remoteAddress); + return CA_STATUS_FAILED; + } + serverInfo->clientHandle = clientHandle; + serverInfo->status = LE_STATUS_CONNECTION_INITIATED; if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress)) { @@ -1540,19 +1576,28 @@ exit: return CA_STATUS_OK; } -bool CALEClientIsConnected(const char* address) +uint16_t CALEClientGetMtuSize(const char* remote_address) { - (void)address; - //@Todo - return true; + 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; + } -uint16_t CALEClientGetMtuSize(const char* address) +bool CALEClientIsConnected(const char* address) { - VERIFY_NON_NULL_RET(address, TAG, "address is null", CA_DEFAULT_BLE_MTU_SIZE); + (void)address; //@Todo - //it should be implemented after update Tizen 3.0 - return CA_DEFAULT_BLE_MTU_SIZE; + return true; } CAResult_t CALEClientSetMtuSize(const char* address, uint16_t mtuSize)