#define MICROSECS_PER_SEC 1000000
#define WAIT_TIME_WRITE_CHARACTERISTIC 10 * MICROSECS_PER_SEC
+//For custom uuid ble server
+#define CA_GATT_CUSTOM_UUID "75004209-0000-0000-0000-000000000000"
+#define CA_GATT_CUSTOM_UUID2 "75004204-0000-0000-0000-000000000000"
+#define CUSTOM_UUID_LEN 9
+
uint64_t const TIMEOUT = 30 * MICROSECS_PER_SEC;
/**
* Reference to threadpool
*/
static ca_thread_pool_t g_LEClientThreadPool = NULL;
+CAResult_t CALEClientScanThread();
void CALEGattCharacteristicChangedCb(bt_gatt_h characteristic,
char *value,
g_isConnectionInProgress = false;
oc_mutex_unlock(g_isConnectionInProgressMutex);
- // Resume the scanning
- oc_mutex_lock(g_scanMutex);
+
+ // TODO:Disabling scanning for now.Need to check.
+ /*oc_mutex_lock(g_scanMutex);
if (g_isMulticastInProgress || g_isUnicastScanInProgress)
{
CAResult_t ret = CALEGattStartDeviceScanning();
}
}
oc_mutex_unlock(g_scanMutex);
-
+*/
LEServerInfo *serverInfo = NULL;
oc_mutex_lock(g_LEServerListMutex);
if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
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]);
}
void *userData)
{
(void)userData;
-
OIC_LOG(DEBUG, TAG, "IN");
VERIFY_NON_NULL_VOID(scanInfo, TAG, "scanInfo");
}
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 (CA_STATUS_OK != ret)
{
- // 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;
- }
- }
-
- 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;
- }
-
- bt_gatt_client_h clientHandle = NULL;
- int32_t ret = bt_gatt_client_create(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(remoteAddress);
- return CA_STATUS_FAILED;
- }
- serverInfo->clientHandle = clientHandle;
-
- 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;
- }
+ OIC_LOG_V(DEBUG, TAG,
+ "Newly discovered device with address [%s] ", scanInfo->remote_address);
- if (isMulticastInProgress)
- {
- if (CA_STATUS_OK != ret)
+ if (CALEIsHaveService(scanInfo, CA_GATT_SERVICE_UUID) ||
+ CALEIsHaveService(scanInfo, CA_GATT_CUSTOM_UUID)||
+ CALEIsHaveService(scanInfo, CA_GATT_CUSTOM_UUID2))
{
- OIC_LOG_V(DEBUG, TAG,
- "Newly discovered device with address [%s], adding to list", scanInfo->remote_address);
+ OIC_LOG_V(DEBUG, TAG, "Device [%s] supports OIC or custom service", scanInfo->remote_address);
char *addr = OICStrdup(scanInfo->remote_address);
if (NULL == addr)
CAFreeLEServerInfo(serverInfo);
return;
}
-
- 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))
- {
- OIC_LOG(ERROR, TAG, "Failed to add to pending list");
- continue;
- }
- }
- oc_mutex_unlock(g_multicastDataListMutex);
- }
-
- 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);
}
}
-
oc_mutex_unlock(g_LEServerListMutex);
OIC_LOG(DEBUG, TAG, "OUT");
}
oc_mutex_unlock(g_LEClientThreadPoolMutex);
return CA_STATUS_FAILED;
}
+
+ result= ca_thread_pool_add_task(g_LEClientThreadPool, CALEClientScanThread,
+ NULL, NULL);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG(ERROR, TAG, "ca_thread_pool_add_task failed");
+ CATerminateGattClientMutexVariables();
+ oc_mutex_unlock(g_LEClientThreadPoolMutex);
+ return CA_STATUS_FAILED;
+ }
oc_mutex_unlock(g_LEClientThreadPoolMutex);
OIC_LOG(DEBUG, TAG, "OUT");
return CA_STATUS_OK;
}
+CAResult_t CALEClientScanThread()
+{
+ oc_mutex_lock(g_scanMutex);
+ if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
+ {
+ CAResult_t result = CALEGattStartDeviceScanning();
+ if (CA_STATUS_OK != result)
+ {
+ oc_mutex_unlock(g_scanMutex);
+ OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning failed");
+ return CA_STATUS_FAILED;
+ }
+ g_isUnicastScanInProgress = true;
+ // Start Timer
+ oc_cond_signal(g_startTimerCond);
+ }
+ else
+ {
+ g_isUnicastScanInProgress = true;
+ // Reset Timer
+ oc_cond_signal(g_scanningTimeCond);
+ }
+ oc_mutex_unlock(g_scanMutex);
+
+ OIC_LOG(DEBUG, TAG, "OUT");
+ return CA_STATUS_OK;
+}
+
void CAStartTimerThread(void *data)
{
(void)data;
void CADiscoverLEServicesThread(void *remoteAddress)
{
OIC_LOG(DEBUG, TAG, "IN");
-
VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
char *address = (char *)remoteAddress;
}
bt_gatt_h serviceHandle = NULL;
- ret = bt_gatt_client_get_service(serverInfo->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,
return CA_STATUS_FAILED;
}
-
//TODO: This data has to be freed while unsetting the callback.
char *addr = OICStrdup(remoteAddress);
if (NULL == addr)
"Device with address [%s] not yet found, initiating scan",
remoteAddress);
- char *addr = OICStrdup(remoteAddress);
- if (NULL == addr)
- {
- oc_mutex_unlock(g_LEServerListMutex);
- OIC_LOG(ERROR, TAG, "Device address is NULL");
- return CA_STATUS_FAILED;
- }
-
- serverInfo = (LEServerInfo *)OICCalloc(1, sizeof(LEServerInfo));
- if (NULL == serverInfo)
- {
- oc_mutex_unlock(g_LEServerListMutex);
- OIC_LOG(ERROR, TAG, "Calloc failed");
- OICFree(addr);
- return CA_STATUS_FAILED;
- }
-
- serverInfo->remoteAddress = addr;
- serverInfo->status = LE_STATUS_UNICAST_PENDING;
-
- 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);
- CAFreeLEServerInfo(serverInfo);
- return CA_STATUS_FAILED;
- }
-
if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
{
oc_mutex_unlock(g_LEServerListMutex);
OIC_LOG(ERROR, TAG, "Could not add data to pending list");
return CA_STATUS_FAILED;
}
-
+
bt_gatt_client_h clientHandle = NULL;
- int32_t ret = bt_gatt_client_create(remoteAddress, &clientHandle);
+ 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(remoteAddress);
+ "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret));
+ CALEGattDisConnect(serverInfo->remoteAddress);
return CA_STATUS_FAILED;
}
serverInfo->clientHandle = clientHandle;