#include "cacommon.h"
#include "camutex.h"
#include "caadapterutils.h"
+#ifndef SINGLE_THREAD
#include "caqueueingthread.h"
+#endif
#include "cafragmentation.h"
#include "oic_malloc.h"
#include "oic_string.h"
+#include "caremotehandler.h"
/**
* @var CALEADAPTER_TAG
* @brief Logging tag for module name.
*/
-#define CALEADAPTER_TAG "CA_BLE_ADAPTER"
+#define CALEADAPTER_TAG "LAD"
/**
* @var g_networkCallback
/**
* @var g_localBLEAddress
- * @brief bleAddress of the local adapter. Value will be initialized to zero, and will be updated later.
+ * @brief bleAddress of the local adapter. Value will be initialized to zero, and will
+ * be updated later.
*/
static char g_localBLEAddress[18] = {0};
static ca_mutex g_bleAdapterThreadPoolMutex = NULL;
/**
- * @var g_bleClientSendQueueHandle
- * @brief Queue to process the outgoing packets from GATTClient.
- */
-static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
-
-/**
- * @var g_bleClientReceiverQueue
- * @brief Queue to process the incoming packets to GATT Client.
- */
-static CAQueueingThread_t *g_bleClientReceiverQueue = NULL;
-
-/**
* @var g_bleClientSendDataMutex
* @brief Mutex to synchronize the queing of the data from SenderQueue.
*/
*/
static ca_mutex g_bleClientReceiveDataMutex = NULL;
-/**
- * @var g_dataReceiverHandlerState
- * @brief Stop condition of recvhandler.
- */
-static bool g_dataReceiverHandlerState = false;
-
-/**
- * @var g_sendQueueHandle
- * @brief Queue to process the outgoing packets from GATTServer.
- */
-static CAQueueingThread_t *g_sendQueueHandle = NULL;
-
-/**
- * @var g_bleServerReceiverQueue
- * @brief Queue to process the incoming packets to GATTServer
- */
-static CAQueueingThread_t *g_bleServerReceiverQueue = NULL;
/**
* @var g_bleServerSendDataMutex
* @fn CASetBleAdapterThreadPoolHandle
* @brief Used to Set the gThreadPool handle which is required for spawning new thread.
*
-* @param[in] handle - Thread pool handle which is given by above layer for using thread creation task.
+* @param[in] handle - Thread pool handle which is given by above layer for using thread
+* creation task.
*
* @return void
*
*/
-void CASetBleAdapterThreadPoolHandle(ca_thread_pool_t handle);
+void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
/**
* @fn CALEDeviceStateChangedCb
* @retval CA_STATUS_FAILED Operation failed
*
*/
-CAResult_t CAInitBleAdapterMutex();
+CAResult_t CAInitLEAdapterMutex();
/**
* @fn CATerminateBleAdapterMutex
*
* @return void
*/
-void CATerminateBleAdapterMutex();
-
-/**
-* @fn CALEDataDestroyer
-* @brief Used to free data
-*
-* @return void
-*/
-static void CALEDataDestroyer(void *data, uint32_t size);
+void CATerminateLEAdapterMutex();
/**
* @fn CALEErrorHandler
static void CALEErrorHandler(const char *remoteAddress, const void *data, uint32_t dataLen,
CAResult_t result);
+#ifndef SINGLE_THREAD
+/**
+ * @var g_dataReceiverHandlerState
+ * @brief Stop condition of recvhandler.
+ */
+static bool g_dataReceiverHandlerState = false;
-CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
- CANetworkPacketReceivedCallback reqRespCallback,
- CANetworkChangeCallback netCallback,
- CAErrorHandleCallback errorCallback, ca_thread_pool_t handle)
+/**
+ * @var g_bleClientSendQueueHandle
+ * @brief Queue to process the outgoing packets from GATTClient.
+ */
+static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL;
+
+/**
+ * @var g_bleClientReceiverQueue
+ * @brief Queue to process the incoming packets to GATT Client.
+ */
+static CAQueueingThread_t *g_bleClientReceiverQueue = NULL;
+
+/**
+ * @var g_bleServerSendQueueHandle
+ * @brief Queue to process the outgoing packets from GATTServer.
+ */
+static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
+
+/**
+ * @var g_bleServerReceiverQueue
+ * @brief Queue to process the incoming packets to GATTServer
+ */
+static CAQueueingThread_t *g_bleServerReceiverQueue = NULL;
+
+/**
+* @fn CALEDataDestroyer
+* @brief Used to free data
+*
+* @return void
+*/
+static void CALEDataDestroyer(void *data, uint32_t size);
+
+void CAInitLEQueues()
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
- //Input validation
- VERIFY_NON_NULL(registerCallback, NULL, "RegisterConnectivity callback is null");
- VERIFY_NON_NULL(reqRespCallback, NULL, "PacketReceived Callback is null");
- VERIFY_NON_NULL(netCallback, NULL, "NetworkChange Callback is null");
-
- CAResult_t result = CAInitBleAdapterMutex();
+ CAResult_t result = CAInitLEServerQueues();
if (CA_STATUS_OK != result)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
- return CA_STATUS_FAILED;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerQueues failed");
+ return;
}
- result = CAInitializeLENetworkMonitor();
+ result = CAInitLEClientQueues();
if (CA_STATUS_OK != result)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
- return CA_STATUS_FAILED;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
+ return;
}
- CAInitializeLEAdapter();
-
- CASetBleClientThreadPoolHandle(handle);
- CASetBleServerThreadPoolHandle(handle);
- CASetBleAdapterThreadPoolHandle(handle);
- CASetBLEReqRespServerCallback(CABLEServerReceivedData);
- CASetBLEReqRespClientCallback(CABLEClientReceivedData);
- CASetBLEReqRespAdapterCallback(reqRespCallback);
-
- CASetBLEClientErrorHandleCallback(CALEErrorHandler);
- CASetBLEServerErrorHandleCallback(CALEErrorHandler);
- CALERegisterNetworkNotifications(netCallback);
-
- g_errorHandler = errorCallback;
-
- CAConnectivityHandler_t connHandler;
- connHandler.startAdapter = CAStartLE;
- connHandler.stopAdapter = CAStopLE;
- connHandler.startListenServer = CAStartLEListeningServer;
- connHandler.startDiscoveryServer = CAStartLEDiscoveryServer;
- connHandler.sendData = CASendLEUnicastData;
- connHandler.sendDataToAll = CASendLEMulticastData;
- connHandler.GetnetInfo = CAGetLEInterfaceInformation;
- connHandler.readData = CAReadLEData;
- connHandler.terminate = CATerminateLE;
- registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
-
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
-
- return CA_STATUS_OK;
-}
-
-CAResult_t CAStartLE()
-{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE, not implemented");
OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
- return CA_STATUS_OK;
}
-CAResult_t CAStopLE()
+CAResult_t CAInitLEServerQueues()
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
- CAStopBleQueues();
+ ca_mutex_lock(g_bleAdapterThreadPoolMutex);
- ca_mutex_lock(g_bleIsServerMutex);
- if (true == g_isServer)
+ CAResult_t result = CAInitLEServerSenderQueue();
+ if (CA_STATUS_OK != result)
{
- CAStopBleGattServer();
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
+ ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
+ return CA_STATUS_FAILED;
}
- else
+
+ result = CAInitLEServerReceiverQueue();
+ if (CA_STATUS_OK != result)
{
- CAStopBLEGattClient();
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerReceiverQueue failed");
+ ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
+ return CA_STATUS_FAILED;
}
- ca_mutex_unlock(g_bleIsServerMutex);
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
+ g_dataReceiverHandlerState = true;
+ ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
+
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
return CA_STATUS_OK;
}
-void CATerminateLE()
+CAResult_t CAInitLEClientQueues()
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
- CASetBLEReqRespServerCallback(NULL);
- CASetBLEReqRespClientCallback(NULL);
- CALERegisterNetworkNotifications(NULL);
- CASetBLEReqRespAdapterCallback(NULL);
- CATerminateLENetworkMonitor();
+ ca_mutex_lock(g_bleAdapterThreadPoolMutex);
- ca_mutex_lock(g_bleIsServerMutex);
- if (true == g_isServer)
+ CAResult_t result = CAInitLEClientSenderQueue();
+ if (CA_STATUS_OK != result)
{
- CATerminateBleGattServer();
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
+ ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
+ return CA_STATUS_FAILED;
}
- else
+
+ result = CAInitLEClientReceiverQueue();
+ if (CA_STATUS_OK != result)
{
- CATerminateBLEGattClient();
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientReceiverQueue failed");
+ ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
+ return CA_STATUS_FAILED;
}
- ca_mutex_unlock(g_bleIsServerMutex);
- CATerminateBleQueues();
+ g_dataReceiverHandlerState = true;
- CATerminateBleAdapterMutex();
+ ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
+ return CA_STATUS_OK;
}
-CAResult_t CAStartLEListeningServer()
+CAResult_t CAInitLEServerSenderQueue()
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
-
- CAResult_t result = CAInitBleServerQueues();
- if (CA_STATUS_OK != result)
+ // Check if the message queue is already initialized
+ if (g_bleServerSendQueueHandle)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
- return CA_STATUS_FAILED;
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
+ return CA_STATUS_OK;
}
- result = CAGetLEAdapterState();
- if (CA_ADAPTER_NOT_ENABLED == result)
+ // Create send message queue
+ g_bleServerSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
+ if (!g_bleServerSendQueueHandle)
{
- gLeServerStatus = CA_LISTENING_SERVER;
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
- return CA_STATUS_OK;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
+ return CA_MEMORY_ALLOC_FAILED;
}
- if (CA_STATUS_FAILED == result)
+ if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerSendQueueHandle,
+ g_bleAdapterThreadPool,
+ CALEServerSendDataThread, CALEDataDestroyer))
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
+ OICFree(g_bleServerSendQueueHandle);
+ g_bleServerSendQueueHandle = NULL;
return CA_STATUS_FAILED;
}
- CAStartBleGattServer();
-
- ca_mutex_lock(g_bleIsServerMutex);
- g_isServer = true;
- ca_mutex_unlock(g_bleIsServerMutex);
+ if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerSendQueueHandle))
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
+ OICFree(g_bleServerSendQueueHandle);
+ g_bleServerSendQueueHandle = NULL;
+ return CA_STATUS_FAILED;
+ }
OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
return CA_STATUS_OK;
}
-CAResult_t CAStartLEDiscoveryServer()
+CAResult_t CAInitLEClientSenderQueue()
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
- CAResult_t result = CAInitBleClientQueues();
- if (CA_STATUS_OK != result)
+ if (g_bleClientSendQueueHandle)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
- return CA_STATUS_FAILED;
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
+ return CA_STATUS_OK;
}
- result = CAGetLEAdapterState();
- if (CA_ADAPTER_NOT_ENABLED == result)
+ // Create send message queue
+ g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
+ if (!g_bleClientSendQueueHandle)
{
- gLeServerStatus = CA_DISCOVERY_SERVER;
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
- return CA_STATUS_OK;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
+ return CA_MEMORY_ALLOC_FAILED;
}
- if (CA_STATUS_FAILED == result)
+ if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
+ g_bleAdapterThreadPool,
+ CALEClientSendDataThread, CALEDataDestroyer))
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
+ OICFree(g_bleClientSendQueueHandle);
+ g_bleClientSendQueueHandle = NULL;
return CA_STATUS_FAILED;
}
- CAStartBLEGattClient();
-
- ca_mutex_lock(g_bleIsServerMutex);
- g_isServer = false;
- ca_mutex_unlock(g_bleIsServerMutex);
+ if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientSendQueueHandle))
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
+ OICFree(g_bleClientSendQueueHandle);
+ g_bleClientSendQueueHandle = NULL;
+ return CA_STATUS_FAILED;
+ }
OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
return CA_STATUS_OK;
}
-CAResult_t CAStartLENotifyServer()
+CAResult_t CAInitLEServerReceiverQueue()
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
+ // Check if the message queue is already initialized
+ if (g_bleServerReceiverQueue)
+ {
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
+ return CA_STATUS_OK;
+ }
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
- return CA_STATUS_OK;
-}
+ // Create send message queue
+ g_bleServerReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
+ if (!g_bleServerReceiverQueue)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
+ OICFree(g_bleServerSendQueueHandle);
+ return CA_MEMORY_ALLOC_FAILED;
+ }
-uint32_t CASendLENotification(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
-{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
+ if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerReceiverQueue, g_bleAdapterThreadPool,
+ CALEServerDataReceiverHandler, CALEDataDestroyer))
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
+ OICFree(g_bleServerReceiverQueue);
+ g_bleServerReceiverQueue = NULL;
+ return CA_STATUS_FAILED;
+ }
+
+ if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerReceiverQueue))
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
+ OICFree(g_bleServerReceiverQueue);
+ g_bleServerReceiverQueue = NULL;
+ return CA_STATUS_FAILED;
+ }
OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
- return 0;
+ return CA_STATUS_OK;
}
-CAResult_t CAReadLEData()
+CAResult_t CAInitLEClientReceiverQueue()
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
- return CA_STATUS_OK;
-}
-
-int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
-{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
-
- //Input validation
- VERIFY_NON_NULL_RET(endpoint, NULL, "Remote endpoint is null", -1);
- VERIFY_NON_NULL_RET(data, NULL, "Data is null", -1);
-
- CAResult_t result = CA_STATUS_FAILED;
-
- ca_mutex_lock(g_bleIsServerMutex);
- if (true == g_isServer)
+ // Check if the message queue is already initialized
+ if (g_bleClientReceiverQueue)
{
- result = CABLEServerSendData(endpoint, data, dataLen);
- if (CA_STATUS_OK != result)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG,
- "[SendLEUnicastData] CABleServerSenderQueueEnqueueMessage failed \n");
- g_errorHandler((void *) endpoint, (void *) data, dataLen, result);
- ca_mutex_unlock(g_bleIsServerMutex);
- return -1;
- }
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
}
else
{
- result = CABLEClientSendData(endpoint, data, dataLen);
- if (CA_STATUS_OK != result)
+ // Create send message queue
+ g_bleClientReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
+ if (!g_bleClientReceiverQueue)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG,
- "[SendLEUnicastData] CABleClientSenderQueueEnqueueMessage failed \n");
- g_errorHandler(endpoint, data, dataLen, result);
- ca_mutex_unlock(g_bleIsServerMutex);
- return -1;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
+ OICFree(g_bleClientSendQueueHandle);
+ return CA_MEMORY_ALLOC_FAILED;
}
- }
- ca_mutex_unlock(g_bleIsServerMutex);
-
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
- return dataLen;
-}
-
-int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
-{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
-
- //Input validation
- VERIFY_NON_NULL_RET(data, NULL, "Data is null", -1);
-
- if (0 >= dataLen)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
- return -1;
- }
- CAResult_t result = CA_STATUS_FAILED;
-
- ca_mutex_lock(g_bleIsServerMutex);
- if (true == g_isServer)
- {
- result = CABLEServerSendData(endpoint, data, dataLen);
- if (CA_STATUS_OK != result)
+ if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientReceiverQueue,
+ g_bleAdapterThreadPool,
+ CALEClientDataReceiverHandler, NULL))
{
- OIC_LOG(ERROR, CALEADAPTER_TAG,
- "[SendLEMulticastDataToAll] CABleServerSenderQueueEnqueueMessage failed" );
- ca_mutex_unlock(g_bleIsServerMutex);
- g_errorHandler(endpoint, data, dataLen, result);
- return -1;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
+ OICFree(g_bleClientSendQueueHandle);
+ OICFree(g_bleClientReceiverQueue);
+ g_bleClientReceiverQueue = NULL;
+ return CA_STATUS_FAILED;
}
}
- else
+ if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientReceiverQueue))
{
- result = CABLEClientSendData(endpoint, data, dataLen);
- if (CA_STATUS_OK != result)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG,
- "[SendLEMulticastDataToAll] CABleClientSenderQueueEnqueueMessage failed" );
- g_errorHandler(endpoint, data, dataLen, result);
- ca_mutex_unlock(g_bleIsServerMutex);
- return -1;
- }
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
+ OICFree(g_bleClientReceiverQueue);
+ g_bleClientReceiverQueue = NULL;
+ return CA_STATUS_FAILED;
}
- ca_mutex_unlock(g_bleIsServerMutex);
OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
- return dataLen;
+ return CA_STATUS_OK;
}
-CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
+void CAStopLEQueues()
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
- VERIFY_NON_NULL(info, NULL, "CALocalConnectivity info is null");
-
- char *local_address = NULL;
-
- CAResult_t res = CAGetLEAddress(&local_address);
- if (CA_STATUS_OK != res)
+ ca_mutex_lock(g_bleClientSendDataMutex);
+ if (NULL != g_bleClientSendQueueHandle)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
- return res;
+ CAQueueingThreadStop(g_bleClientSendQueueHandle);
}
+ ca_mutex_unlock(g_bleClientSendDataMutex);
- if (NULL == local_address)
+ ca_mutex_lock(g_bleClientReceiveDataMutex);
+ if (NULL != g_bleClientReceiverQueue)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
- return CA_STATUS_FAILED;
+ CAQueueingThreadStop(g_bleClientReceiverQueue);
}
+ ca_mutex_unlock(g_bleClientReceiveDataMutex);
- *size = 0;
- (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof (CAEndpoint_t));
- if (NULL == (*info))
+ ca_mutex_lock(g_bleServerSendDataMutex);
+ if (NULL != g_bleServerSendQueueHandle)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
- OICFree(local_address);
- return CA_STATUS_FAILED;
+ CAQueueingThreadStop(g_bleServerSendQueueHandle);
}
+ ca_mutex_unlock(g_bleServerSendDataMutex);
- size_t local_address_len = strlen(local_address);
-
- if(local_address_len >= sizeof(g_localBLEAddress) ||
- local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
+ ca_mutex_lock(g_bleServerReceiveDataMutex);
+ if (NULL != g_bleServerReceiverQueue)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
- OICFree(*info);
- OICFree(local_address);
- return CA_STATUS_FAILED;
+ CAQueueingThreadStop(g_bleServerReceiverQueue);
}
-
- OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
- ca_mutex_lock(g_bleLocalAddressMutex);
- OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
- ca_mutex_unlock(g_bleLocalAddressMutex);
-
- (*info)->adapter = CA_ADAPTER_GATT_BTLE;
- *size = 1;
- OICFree(local_address);
+ ca_mutex_unlock(g_bleServerReceiveDataMutex);
OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
- return CA_STATUS_OK;
}
-CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
+void CATerminateLEQueues()
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
- ca_mutex_lock(g_bleNetworkCbMutex);
- g_networkCallback = netCallback;
- ca_mutex_unlock(g_bleNetworkCbMutex);
- CAResult_t res = CA_STATUS_OK;
- if (netCallback)
- {
- res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
- if (CA_STATUS_OK != res)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
- }
- }
- else
- {
- res = CAUnSetLEAdapterStateChangedCb();
- if (CA_STATUS_OK != res)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
- }
- }
+ CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
+ OICFree(g_bleClientSendQueueHandle);
+ g_bleClientSendQueueHandle = NULL;
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
- return res;
-}
-void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
-{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
+ CAQueueingThreadDestroy(g_bleClientReceiverQueue);
+ OICFree(g_bleClientReceiverQueue);
+ g_bleClientReceiverQueue = NULL;
- VERIFY_NON_NULL_VOID(g_localBLEAddress, NULL, "g_localBLEAddress is null");
- CAEndpoint_t localEndpoint = {};
- localEndpoint.adapter = CA_ADAPTER_GATT_BTLE;
- ca_mutex_lock(g_bleLocalAddressMutex);
- OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), g_localBLEAddress);
- ca_mutex_unlock(g_bleLocalAddressMutex);
- g_bleAdapterState = adapter_state;
- // Start a GattServer/Client if gLeServerStatus is SET
- if (CA_LISTENING_SERVER == gLeServerStatus)
- {
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattServer");
- CAStartBleGattServer();
- }
- else if (CA_DISCOVERY_SERVER == gLeServerStatus)
- {
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
- CAStartBLEGattClient();
- }
- gLeServerStatus = CA_SERVER_NOTSTARTED;
+ CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
+ OICFree(g_bleServerSendQueueHandle);
+ g_bleServerSendQueueHandle = NULL;
- ca_mutex_lock(g_bleNetworkCbMutex);
- if (NULL != g_networkCallback)
- {
- g_networkCallback(&localEndpoint, adapter_state);
- }
- else
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
- }
- ca_mutex_unlock(g_bleNetworkCbMutex);
+
+ CAQueueingThreadDestroy(g_bleServerReceiverQueue);
+ OICFree(g_bleServerReceiverQueue);
+ g_bleServerReceiverQueue = NULL;
OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
}
-CAResult_t CAInitBleAdapterMutex()
+void CALEServerDataReceiverHandler(void *threadData)
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
- if (NULL == g_bleIsServerMutex)
- {
- g_bleIsServerMutex = ca_mutex_new();
- if (NULL == g_bleIsServerMutex)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
- return CA_STATUS_FAILED;
- }
- }
+ static uint32_t recvDataLen = 0;
+ static uint32_t totalDataLen = 0;
+ static char *defragData = NULL;
+ static bool isHeaderAvailable = false;
+ static CAEndpoint_t *remoteEndpoint = NULL;
- if (NULL == g_bleNetworkCbMutex)
- {
- g_bleNetworkCbMutex = ca_mutex_new();
- if (NULL == g_bleNetworkCbMutex)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
- return CA_STATUS_FAILED;
- }
- }
+ ca_mutex_lock(g_bleServerReceiveDataMutex);
- if (NULL == g_bleLocalAddressMutex)
+ if (g_dataReceiverHandlerState)
{
- g_bleLocalAddressMutex = ca_mutex_new();
- if (NULL == g_bleLocalAddressMutex)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
- return CA_STATUS_FAILED;
- }
- }
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
- if (NULL == g_bleAdapterThreadPoolMutex)
- {
- g_bleAdapterThreadPoolMutex = ca_mutex_new();
- if (NULL == g_bleAdapterThreadPoolMutex)
+ CALEData_t *bleData = (CALEData_t *) threadData;
+ if (!bleData)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
- return CA_STATUS_FAILED;
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
+ return;
}
- }
- if (NULL == g_bleClientSendDataMutex)
- {
- g_bleClientSendDataMutex = ca_mutex_new();
- if (NULL == g_bleClientSendDataMutex)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
- return CA_STATUS_FAILED;
- }
- }
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
- if (NULL == g_bleClientReceiveDataMutex)
- {
- g_bleClientReceiveDataMutex = ca_mutex_new();
- if (NULL == g_bleClientReceiveDataMutex)
+ if (!isHeaderAvailable)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
- return CA_STATUS_FAILED;
- }
- }
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
+ totalDataLen = CAParseHeader((char*)bleData->data);
- if (NULL == g_bleServerSendDataMutex)
- {
- g_bleServerSendDataMutex = ca_mutex_new();
- if (NULL == g_bleServerSendDataMutex)
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes", totalDataLen);
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%d] bytes", bleData->dataLen);
+
+ defragData = (char *) OICCalloc(totalDataLen + 1, sizeof(char));
+ if (NULL == defragData)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
+ return;
+ }
+
+ const char *remoteAddress = bleData->remoteEndpoint->addr;
+
+ remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_GATT_BTLE,
+ remoteAddress, 0);
+
+ memcpy(defragData + recvDataLen, bleData->data + CA_HEADER_LENGTH,
+ bleData->dataLen - CA_HEADER_LENGTH);
+ recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
+ isHeaderAvailable = true;
+ }
+ else
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
- return CA_STATUS_FAILED;
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
+ memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
+ recvDataLen += bleData->dataLen ;
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
+ totalDataLen, recvDataLen);
}
- }
-
- if (NULL == g_bleServerReceiveDataMutex)
- {
- g_bleServerReceiveDataMutex = ca_mutex_new();
- if (NULL == g_bleServerReceiveDataMutex)
+ if (totalDataLen == recvDataLen)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
- return CA_STATUS_FAILED;
+ ca_mutex_lock(g_bleAdapterReqRespCbMutex);
+ if (NULL == g_networkPacketReceivedCallback)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
+ ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
+ return;
+ }
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
+ g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
+ recvDataLen = 0;
+ totalDataLen = 0;
+ isHeaderAvailable = false;
+ remoteEndpoint = NULL;
+ defragData = NULL;
+ ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
}
- }
- if (NULL == g_bleAdapterReqRespCbMutex)
- {
- g_bleAdapterReqRespCbMutex = ca_mutex_new();
- if (NULL == g_bleAdapterReqRespCbMutex)
+ if (false == g_dataReceiverHandlerState)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
- return CA_STATUS_FAILED;
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
+ recvDataLen = 0;
+ totalDataLen = 0;
+ isHeaderAvailable = false;
+ OICFree(defragData);
+ CAFreeEndpoint(remoteEndpoint);
+ ca_mutex_unlock(g_bleServerReceiveDataMutex);
+ return;
}
}
-
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
- return CA_STATUS_OK;
+ ca_mutex_unlock(g_bleServerReceiveDataMutex);
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
}
-void CATerminateBleAdapterMutex()
+void CALEClientDataReceiverHandler(void *threadData)
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
- ca_mutex_free(g_bleIsServerMutex);
- g_bleIsServerMutex = NULL;
-
- ca_mutex_free(g_bleNetworkCbMutex);
- g_bleNetworkCbMutex = NULL;
+ static const char *remoteAddress = NULL;
+ static uint32_t recvDataLen = 0;
+ static uint32_t totalDataLen = 0;
+ static char *defragData = NULL;
+ static bool isHeaderAvailable = false;
+ static CAEndpoint_t *remoteEndpoint = NULL;
- ca_mutex_free(g_bleLocalAddressMutex);
- g_bleLocalAddressMutex = NULL;
+ ca_mutex_lock(g_bleClientReceiveDataMutex);
- ca_mutex_free(g_bleAdapterThreadPoolMutex);
- g_bleAdapterThreadPoolMutex = NULL;
+ if (g_dataReceiverHandlerState)
+ {
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
- ca_mutex_free(g_bleClientSendDataMutex);
- g_bleClientSendDataMutex = NULL;
+ CALEData_t *bleData = (CALEData_t *) threadData;
+ if (!bleData)
+ {
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid wifidata!");
+ return;
+ }
- ca_mutex_free(g_bleClientReceiveDataMutex);
- g_bleClientReceiveDataMutex = NULL;
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
- ca_mutex_free(g_bleServerSendDataMutex);
- g_bleServerSendDataMutex = NULL;
+ if (!isHeaderAvailable)
+ {
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
- ca_mutex_free(g_bleServerReceiveDataMutex);
- g_bleServerReceiveDataMutex = NULL;
+ totalDataLen = CAParseHeader(bleData->data);
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes",
+ totalDataLen);
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received in the first packet [%d] bytes",
+ bleData->dataLen);
- ca_mutex_free(g_bleAdapterReqRespCbMutex);
- g_bleAdapterReqRespCbMutex = NULL;
+ defragData = (char *) OICMalloc(sizeof(char) * totalDataLen);
+ if (NULL == defragData)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
+ return;
+ }
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
-}
+ remoteAddress = bleData->remoteEndpoint->addr;
-void CAInitBleQueues()
-{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
+ remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_GATT_BTLE,
+ remoteAddress, 0);
- CAResult_t result = CAInitBleServerQueues();
- if (CA_STATUS_OK != result)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerQueues failed");
- return;
- }
+ memcpy(defragData, bleData->data + CA_HEADER_LENGTH,
+ bleData->dataLen - CA_HEADER_LENGTH);
+ recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
+ isHeaderAvailable = true;
+ }
+ else
+ {
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
+ memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
+ recvDataLen += bleData->dataLen ;
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
+ totalDataLen, recvDataLen);
+ }
+ if (totalDataLen == recvDataLen)
+ {
+ ca_mutex_lock(g_bleAdapterReqRespCbMutex);
+ if (NULL == g_networkPacketReceivedCallback)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
+ ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
+ return;
+ }
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
+ g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
+ recvDataLen = 0;
+ totalDataLen = 0;
+ isHeaderAvailable = false;
+ remoteEndpoint = NULL;
+ defragData = NULL;
+ ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
+ }
- result = CAInitBleClientQueues();
- if (CA_STATUS_OK != result)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientQueues failed");
- return;
+ if (false == g_dataReceiverHandlerState)
+ {
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
+ OICFree(defragData);
+ CAFreeEndpoint(remoteEndpoint);
+ ca_mutex_unlock(g_bleClientReceiveDataMutex);
+ return;
+ }
}
-
+ ca_mutex_unlock(g_bleClientReceiveDataMutex);
OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
}
-CAResult_t CAInitBleServerQueues()
+void CALEServerSendDataThread(void *threadData)
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
- ca_mutex_lock(g_bleAdapterThreadPoolMutex);
-
- CAResult_t result = CAInitBleServerSenderQueue();
- if (CA_STATUS_OK != result)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
- ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
- return CA_STATUS_FAILED;
- }
-
- result = CAInitBleServerReceiverQueue();
- if (CA_STATUS_OK != result)
+ CALEData_t *bleData = (CALEData_t *) threadData;
+ if (!bleData)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerReceiverQueue failed");
- ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
- return CA_STATUS_FAILED;
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
+ return;
}
- g_dataReceiverHandlerState = true;
-
- ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
-
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
- return CA_STATUS_OK;
-}
-
-CAResult_t CAInitBleClientQueues()
-{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
+ char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
+ VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
- ca_mutex_lock(g_bleAdapterThreadPoolMutex);
+ int32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
- CAResult_t result = CAInitBleClientSenderQueue();
- if (CA_STATUS_OK != result)
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server total Data length with header is [%d]", totalLength);
+ char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
+ if (NULL == dataSegment)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
- ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
- return CA_STATUS_FAILED;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
+ OICFree(header);
+ return;
}
- result = CAInitBleClientReceiverQueue();
- if (CA_STATUS_OK != result)
+ CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
+ if (CA_STATUS_OK != result )
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientReceiverQueue failed");
- ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
- return CA_STATUS_FAILED;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
+ OICFree(header);
+ OICFree(dataSegment);
+ return ;
}
- g_dataReceiverHandlerState = true;
-
- ca_mutex_unlock(g_bleAdapterThreadPoolMutex);
-
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
- return CA_STATUS_OK;
-}
+ memcpy(dataSegment, header, CA_HEADER_LENGTH);
+ OICFree(header);
-CAResult_t CAInitBleServerSenderQueue()
-{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
- // Check if the message queue is already initialized
- if (g_sendQueueHandle)
+ int32_t length = 0;
+ if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Queue is already initialized!");
- return CA_STATUS_OK;
+ length = totalLength;
+ memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
}
-
- // Create send message queue
- g_sendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
- if (!g_sendQueueHandle)
+ else
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
- return CA_MEMORY_ALLOC_FAILED;
+ length = CA_SUPPORTED_BLE_MTU_SIZE;
+ memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
+ CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
}
- if (CA_STATUS_OK != CAQueueingThreadInitialize(g_sendQueueHandle, g_bleAdapterThreadPool,
- CABLEServerSendDataThread, CALEDataDestroyer))
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
- OICFree(g_sendQueueHandle);
- g_sendQueueHandle = NULL;
- return CA_STATUS_FAILED;
- }
+ int32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
+ int32_t index = 0;
+ // Send the first segment with the header.
+ if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
+ {
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
+ result = CAUpdateCharacteristicsToGattClient(
+ bleData->remoteEndpoint->addr, dataSegment, length);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
+ g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
+ OICFree(dataSegment);
+ return;
+ }
+
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
+ for (index = 1; index < iter; index++)
+ {
+ // Send the remaining header.
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
+ result = CAUpdateCharacteristicsToGattClient(
+ bleData->remoteEndpoint->addr,
+ bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
+ CA_SUPPORTED_BLE_MTU_SIZE);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+ "Update characteristics failed, result [%d]", result);
+ g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
+ OICFree(dataSegment);
+ return;
+ }
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
+ CA_SUPPORTED_BLE_MTU_SIZE);
+ }
- if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle))
+ int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
+ if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
+ {
+ // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
+ result = CAUpdateCharacteristicsToGattClient(
+ bleData->remoteEndpoint->addr,
+ bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
+ remainingLen);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
+ result);
+ g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
+ OICFree(dataSegment);
+ return;
+ }
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
+ }
+ }
+ else
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
- OICFree(g_sendQueueHandle);
- g_sendQueueHandle = NULL;
- return CA_STATUS_FAILED;
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
+ result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
+ result);
+ CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
+ OICFree(dataSegment);
+ return;
+ }
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
+ for (index = 1; index < iter; index++)
+ {
+ // Send the remaining header.
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
+ result = CAUpdateCharacteristicsToAllGattClients(
+ bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
+ CA_SUPPORTED_BLE_MTU_SIZE);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
+ result);
+ CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
+ OICFree(dataSegment);
+ return;
+ }
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
+ }
+
+ int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
+ if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
+ {
+ // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
+ result = CAUpdateCharacteristicsToAllGattClients(
+ bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
+ remainingLen);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
+ result);
+ CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
+ OICFree(dataSegment);
+ return;
+ }
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
+ }
}
+ OICFree(dataSegment);
OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
- return CA_STATUS_OK;
}
-CAResult_t CAInitBleClientSenderQueue()
+void CALEClientSendDataThread(void *threadData)
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
- if (g_bleClientSendQueueHandle)
- {
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
- return CA_STATUS_OK;
- }
-
- // Create send message queue
- g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
- if (!g_bleClientSendQueueHandle)
+ CALEData_t *bleData = (CALEData_t *) threadData;
+ if (!bleData)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
- return CA_MEMORY_ALLOC_FAILED;
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
+ return;
}
- if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle, g_bleAdapterThreadPool,
- CABLEClientSendDataThread, CALEDataDestroyer))
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
- OICFree(g_bleClientSendQueueHandle);
- g_bleClientSendQueueHandle = NULL;
- return CA_STATUS_FAILED;
- }
+ char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
+ VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
- if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientSendQueueHandle))
+ uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
+ char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
+ if (NULL == dataSegment)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
- OICFree(g_bleClientSendQueueHandle);
- g_bleClientSendQueueHandle = NULL;
- return CA_STATUS_FAILED;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
+ OICFree(header);
+ return;
}
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
- return CA_STATUS_OK;
-}
-
-CAResult_t CAInitBleServerReceiverQueue()
-{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
- // Check if the message queue is already initialized
- if (g_bleServerReceiverQueue)
+ CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
+ if (CA_STATUS_OK != result )
{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
- return CA_STATUS_OK;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
+ OICFree(header);
+ OICFree(dataSegment);
+ return ;
}
+ memcpy(dataSegment, header, CA_HEADER_LENGTH);
+ OICFree(header);
- // Create send message queue
- g_bleServerReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
- if (!g_bleServerReceiverQueue)
+ uint32_t length = 0;
+ if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
- OICFree(g_sendQueueHandle);
- return CA_MEMORY_ALLOC_FAILED;
+ length = totalLength;
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
+ memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
}
-
- if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerReceiverQueue, g_bleAdapterThreadPool,
- CABLEServerDataReceiverHandler, CALEDataDestroyer))
+ else
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
- OICFree(g_bleServerReceiverQueue);
- g_bleServerReceiverQueue = NULL;
- return CA_STATUS_FAILED;
+ length = CA_SUPPORTED_BLE_MTU_SIZE;
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
+ memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
+ CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
}
- if (CA_STATUS_OK != CAQueueingThreadStart(g_bleServerReceiverQueue))
+ uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
+ uint32_t index = 0;
+ if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
- OICFree(g_bleServerReceiverQueue);
- g_bleServerReceiverQueue = NULL;
- return CA_STATUS_FAILED;
- }
-
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
- return CA_STATUS_OK;
-}
-
-CAResult_t CAInitBleClientReceiverQueue()
-{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
+ // Send the first segment with the header.
+ result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addr,
+ dataSegment,
+ length,
+ LE_UNICAST, 0);
- // Check if the message queue is already initialized
- if (g_bleClientReceiverQueue)
- {
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
- }
- else
- {
- // Create send message queue
- g_bleClientReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
- if (!g_bleClientReceiverQueue)
+ if (CA_STATUS_OK != result)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
- OICFree(g_bleClientSendQueueHandle);
- return CA_MEMORY_ALLOC_FAILED;
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
+ g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
+ OICFree(dataSegment);
+ return ;
}
- if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientReceiverQueue, g_bleAdapterThreadPool,
- CABLEClientDataReceiverHandler, NULL))
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
+ for (index = 1; index < iter; index++)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
- OICFree(g_bleClientSendQueueHandle);
- OICFree(g_bleClientReceiverQueue);
- g_bleClientReceiverQueue = NULL;
- return CA_STATUS_FAILED;
+ // Send the remaining header.
+ result = CAUpdateCharacteristicsToGattServer(
+ bleData->remoteEndpoint->addr,
+ bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
+ CA_SUPPORTED_BLE_MTU_SIZE,
+ LE_UNICAST, 0);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
+ result);
+ g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
+ OICFree(dataSegment);
+ return;
+ }
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
+ CA_SUPPORTED_BLE_MTU_SIZE);
}
- }
- if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientReceiverQueue))
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
- OICFree(g_bleClientReceiverQueue);
- g_bleClientReceiverQueue = NULL;
- return CA_STATUS_FAILED;
- }
-
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
- return CA_STATUS_OK;
-}
-void CAStopBleQueues()
-{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
+ uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
+ if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
+ {
+ // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
+ result = CAUpdateCharacteristicsToGattServer(
+ bleData->remoteEndpoint->addr,
+ bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
+ remainingLen,
+ LE_UNICAST, 0);
- ca_mutex_lock(g_bleClientSendDataMutex);
- if (NULL != g_bleClientSendQueueHandle)
- {
- CAQueueingThreadStop(g_bleClientSendQueueHandle);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
+ result);
+ g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
+ OICFree(dataSegment);
+ return;
+ }
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
+ }
}
- ca_mutex_unlock(g_bleClientSendDataMutex);
-
- ca_mutex_lock(g_bleClientReceiveDataMutex);
- if (NULL != g_bleClientReceiverQueue)
+ else
{
- CAQueueingThreadStop(g_bleClientReceiverQueue);
+ //Sending Mulitcast Data
+ // Send the first segment with the header.
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
+ result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+ "Update characteristics (all) failed, result [%d]", result);
+ CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
+ OICFree(dataSegment);
+ return ;
+ }
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
+ // Send the remaining header.
+ for (index = 1; index < iter; index++)
+ {
+ result = CAUpdateCharacteristicsToAllGattServers(
+ bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
+ CA_SUPPORTED_BLE_MTU_SIZE);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]",
+ result);
+ CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
+ OICFree(dataSegment);
+ return;
+ }
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
+ CA_SUPPORTED_BLE_MTU_SIZE);
+ }
+
+ uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
+ if ( remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
+ {
+ // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
+ result = CAUpdateCharacteristicsToAllGattServers(
+ bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
+ remainingLen);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+ "Update characteristics (all) failed, result [%d]", result);
+ CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
+ OICFree(dataSegment);
+ return;
+ }
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
+ }
+
}
- ca_mutex_unlock(g_bleClientReceiveDataMutex);
- ca_mutex_lock(g_bleServerSendDataMutex);
- if (NULL != g_sendQueueHandle)
+ OICFree(dataSegment);
+
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
+}
+
+CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint, const void *data,
+ uint32_t dataLength)
+{
+ CALEData_t *bleData = (CALEData_t *) OICMalloc(sizeof(CALEData_t));
+ if (!bleData)
{
- CAQueueingThreadStop(g_sendQueueHandle);
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
+ return NULL;
}
- ca_mutex_unlock(g_bleServerSendDataMutex);
- ca_mutex_lock(g_bleServerReceiveDataMutex);
- if (NULL != g_bleServerReceiverQueue)
+ bleData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
+ bleData->data = (void *)OICCalloc(dataLength + 1, 1);
+ if (NULL == bleData->data)
{
- CAQueueingThreadStop(g_bleServerReceiverQueue);
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
+ CAFreeLEData(bleData);
+ return NULL;
}
- ca_mutex_unlock(g_bleServerReceiveDataMutex);
+ memcpy(bleData->data, data, dataLength);
+ bleData->dataLen = dataLength;
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
+ return bleData;
}
-void CATerminateBleQueues()
+void CAFreeLEData(CALEData_t *bleData)
{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
-
- CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
- OICFree(g_bleClientSendQueueHandle);
- g_bleClientSendQueueHandle = NULL;
-
-
- CAQueueingThreadDestroy(g_bleClientReceiverQueue);
- OICFree(g_bleClientReceiverQueue);
- g_bleClientReceiverQueue = NULL;
-
-
- CAQueueingThreadDestroy(g_sendQueueHandle);
- OICFree(g_sendQueueHandle);
- g_sendQueueHandle = NULL;
+ VERIFY_NON_NULL_VOID(bleData, NULL, "Param bleData is NULL");
+ CAFreeEndpoint(bleData->remoteEndpoint);
+ OICFree(bleData->data);
+ OICFree(bleData);
+}
- CAQueueingThreadDestroy(g_bleServerReceiverQueue);
- OICFree(g_bleServerReceiverQueue);
- g_bleServerReceiverQueue = NULL;
+void CALEDataDestroyer(void *data, uint32_t size)
+{
+ CALEData_t *ledata = (CALEData_t *) data;
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
+ CAFreeLEData(ledata);
}
-void CABLEServerDataReceiverHandler(void *threadData)
+#endif
+
+CAResult_t CAInitLEAdapterMutex()
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
- static uint32_t recvDataLen = 0;
- static uint32_t totalDataLen = 0;
- static char *defragData = NULL;
- static bool isHeaderAvailable = false;
- static CAEndpoint_t *remoteEndpoint = NULL;
-
- ca_mutex_lock(g_bleServerReceiveDataMutex);
-
- if (g_dataReceiverHandlerState)
+ if (NULL == g_bleIsServerMutex)
{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
-
- CALEData_t *bleData = (CALEData_t *) threadData;
- if (!bleData)
+ g_bleIsServerMutex = ca_mutex_new();
+ if (NULL == g_bleIsServerMutex)
{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
- return;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
+ return CA_STATUS_FAILED;
}
+ }
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
-
- if (!isHeaderAvailable)
- {
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
- totalDataLen = CAParseHeader((char*)bleData->data);
-
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes", totalDataLen);
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%d] bytes", bleData->dataLen);
-
- defragData = (char *) OICCalloc(totalDataLen + 1, sizeof(char));
- if (NULL == defragData)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
- return;
- }
-
- const char *remoteAddress = bleData->remoteEndpoint->addr;
-
- remoteEndpoint = CAAdapterCreateEndpoint(0, CA_ADAPTER_GATT_BTLE, remoteAddress, 0);
-
- memcpy(defragData + recvDataLen, bleData->data + CA_HEADER_LENGTH,
- bleData->dataLen - CA_HEADER_LENGTH);
- recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
- isHeaderAvailable = true;
- }
- else
+ if (NULL == g_bleNetworkCbMutex)
+ {
+ g_bleNetworkCbMutex = ca_mutex_new();
+ if (NULL == g_bleNetworkCbMutex)
{
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
- memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
- recvDataLen += bleData->dataLen ;
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
- totalDataLen, recvDataLen);
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
+ return CA_STATUS_FAILED;
}
- if (totalDataLen == recvDataLen)
+ }
+
+ if (NULL == g_bleLocalAddressMutex)
+ {
+ g_bleLocalAddressMutex = ca_mutex_new();
+ if (NULL == g_bleLocalAddressMutex)
{
- ca_mutex_lock(g_bleAdapterReqRespCbMutex);
- if (NULL == g_networkPacketReceivedCallback)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
- ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
- return;
- }
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
- g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
- recvDataLen = 0;
- totalDataLen = 0;
- isHeaderAvailable = false;
- remoteEndpoint = NULL;
- defragData = NULL;
- ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
+ return CA_STATUS_FAILED;
}
+ }
- if (false == g_dataReceiverHandlerState)
+ if (NULL == g_bleAdapterThreadPoolMutex)
+ {
+ g_bleAdapterThreadPoolMutex = ca_mutex_new();
+ if (NULL == g_bleAdapterThreadPoolMutex)
{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
- recvDataLen = 0;
- totalDataLen = 0;
- isHeaderAvailable = false;
- OICFree(defragData);
- CAAdapterFreeEndpoint(remoteEndpoint);
- ca_mutex_unlock(g_bleServerReceiveDataMutex);
- return;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
+ return CA_STATUS_FAILED;
}
}
- ca_mutex_unlock(g_bleServerReceiveDataMutex);
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
-}
-
-void CABLEClientDataReceiverHandler(void *threadData)
-{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
-
- static const char *remoteAddress = NULL;
- static uint32_t recvDataLen = 0;
- static uint32_t totalDataLen = 0;
- static char *defragData = NULL;
- static bool isHeaderAvailable = false;
- static CAEndpoint_t *remoteEndpoint = NULL;
-
- ca_mutex_lock(g_bleClientReceiveDataMutex);
- if (g_dataReceiverHandlerState)
+ if (NULL == g_bleClientSendDataMutex)
{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
-
- CALEData_t *bleData = (CALEData_t *) threadData;
- if (!bleData)
+ g_bleClientSendDataMutex = ca_mutex_new();
+ if (NULL == g_bleClientSendDataMutex)
{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid wifidata!");
- return;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
+ return CA_STATUS_FAILED;
}
+ }
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation");
-
- if (!isHeaderAvailable)
+ if (NULL == g_bleClientReceiveDataMutex)
+ {
+ g_bleClientReceiveDataMutex = ca_mutex_new();
+ if (NULL == g_bleClientReceiveDataMutex)
{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
-
- totalDataLen = CAParseHeader(bleData->data);
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%d] bytes",
- totalDataLen);
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received in the first packet [%d] bytes",
- bleData->dataLen);
-
- defragData = (char *) OICMalloc(sizeof(char) * totalDataLen);
- if (NULL == defragData)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
- return;
- }
-
- remoteAddress = bleData->remoteEndpoint->addr;
-
- remoteEndpoint = CAAdapterCreateEndpoint(0, CA_ADAPTER_GATT_BTLE, remoteAddress, 0);
-
- memcpy(defragData, bleData->data + CA_HEADER_LENGTH,
- bleData->dataLen - CA_HEADER_LENGTH);
- recvDataLen += bleData->dataLen - CA_HEADER_LENGTH;
- isHeaderAvailable = true;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
+ return CA_STATUS_FAILED;
}
- else
+ }
+
+ if (NULL == g_bleServerSendDataMutex)
+ {
+ g_bleServerSendDataMutex = ca_mutex_new();
+ if (NULL == g_bleServerSendDataMutex)
{
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", bleData->dataLen);
- memcpy(defragData + recvDataLen, bleData->data, bleData->dataLen);
- recvDataLen += bleData->dataLen ;
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] recveived Datalen [%d]",
- totalDataLen, recvDataLen);
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
+ return CA_STATUS_FAILED;
}
- if (totalDataLen == recvDataLen)
+ }
+
+ if (NULL == g_bleServerReceiveDataMutex)
+ {
+ g_bleServerReceiveDataMutex = ca_mutex_new();
+ if (NULL == g_bleServerReceiveDataMutex)
{
- ca_mutex_lock(g_bleAdapterReqRespCbMutex);
- if (NULL == g_networkPacketReceivedCallback)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
- ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
- return;
- }
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending data up !");
- g_networkPacketReceivedCallback(remoteEndpoint, defragData, recvDataLen);
- recvDataLen = 0;
- totalDataLen = 0;
- isHeaderAvailable = false;
- remoteEndpoint = NULL;
- defragData = NULL;
- ca_mutex_unlock(g_bleAdapterReqRespCbMutex);
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
+ return CA_STATUS_FAILED;
}
+ }
- if (false == g_dataReceiverHandlerState)
+ if (NULL == g_bleAdapterReqRespCbMutex)
+ {
+ g_bleAdapterReqRespCbMutex = ca_mutex_new();
+ if (NULL == g_bleAdapterReqRespCbMutex)
{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "GATTClient is terminating. Cleaning up");
- OICFree(defragData);
- CAAdapterFreeEndpoint(remoteEndpoint);
- ca_mutex_unlock(g_bleClientReceiveDataMutex);
- return;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed");
+ return CA_STATUS_FAILED;
}
}
- ca_mutex_unlock(g_bleClientReceiveDataMutex);
+
OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
+ return CA_STATUS_OK;
}
-void CABLEServerSendDataThread(void *threadData)
+void CATerminateLEAdapterMutex()
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
- CALEData_t *bleData = (CALEData_t *) threadData;
- if (!bleData)
- {
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
- return;
- }
+ ca_mutex_free(g_bleIsServerMutex);
+ g_bleIsServerMutex = NULL;
- char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
- VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
+ ca_mutex_free(g_bleNetworkCbMutex);
+ g_bleNetworkCbMutex = NULL;
- int32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
+ ca_mutex_free(g_bleLocalAddressMutex);
+ g_bleLocalAddressMutex = NULL;
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server total Data length with header is [%d]", totalLength);
- char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
- if (NULL == dataSegment)
+ ca_mutex_free(g_bleAdapterThreadPoolMutex);
+ g_bleAdapterThreadPoolMutex = NULL;
+
+ ca_mutex_free(g_bleClientSendDataMutex);
+ g_bleClientSendDataMutex = NULL;
+
+ ca_mutex_free(g_bleClientReceiveDataMutex);
+ g_bleClientReceiveDataMutex = NULL;
+
+ ca_mutex_free(g_bleServerSendDataMutex);
+ g_bleServerSendDataMutex = NULL;
+
+ ca_mutex_free(g_bleServerReceiveDataMutex);
+ g_bleServerReceiveDataMutex = NULL;
+
+ ca_mutex_free(g_bleAdapterReqRespCbMutex);
+ g_bleAdapterReqRespCbMutex = NULL;
+
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
+}
+
+CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
+ CANetworkPacketReceivedCallback reqRespCallback,
+ CANetworkChangeCallback netCallback,
+ CAErrorHandleCallback errorCallback, ca_thread_pool_t handle)
+{
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
+
+ //Input validation
+ VERIFY_NON_NULL(registerCallback, NULL, "RegisterConnectivity callback is null");
+ VERIFY_NON_NULL(reqRespCallback, NULL, "PacketReceived Callback is null");
+ VERIFY_NON_NULL(netCallback, NULL, "NetworkChange Callback is null");
+
+ CAResult_t result = CA_STATUS_OK;
+ result = CAInitLEAdapterMutex();
+ if (CA_STATUS_OK != result)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
- OICFree(header);
- return;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleAdapterMutex failed!");
+ return CA_STATUS_FAILED;
}
-
- CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
- if (CA_STATUS_OK != result )
+ result = CAInitializeLENetworkMonitor();
+ if (CA_STATUS_OK != result)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
- OICFree(header);
- OICFree(dataSegment);
- return ;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
+ return CA_STATUS_FAILED;
}
- memcpy(dataSegment, header, CA_HEADER_LENGTH);
- OICFree(header);
+ CAInitializeLEAdapter();
- int32_t length = 0;
- if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
+ CASetLEClientThreadPoolHandle(handle);
+ CASetLEReqRespClientCallback(CALEAdapterClientReceivedData);
+ CASetLEServerThreadPoolHandle(handle);
+ CASetLEAdapterThreadPoolHandle(handle);
+ CASetLEReqRespServerCallback(CALEAdapterServerReceivedData);
+ CASetLEReqRespAdapterCallback(reqRespCallback);
+
+ CASetBLEClientErrorHandleCallback(CALEErrorHandler);
+ CASetBLEServerErrorHandleCallback(CALEErrorHandler);
+ CALERegisterNetworkNotifications(netCallback);
+
+ g_errorHandler = errorCallback;
+
+ CAConnectivityHandler_t connHandler;
+ connHandler.startAdapter = CAStartLE;
+ connHandler.stopAdapter = CAStopLE;
+ connHandler.startListenServer = CAStartLEListeningServer;
+ connHandler.startDiscoveryServer = CAStartLEDiscoveryServer;
+ connHandler.sendData = CASendLEUnicastData;
+ connHandler.sendDataToAll = CASendLEMulticastData;
+ connHandler.GetnetInfo = CAGetLEInterfaceInformation;
+ connHandler.readData = CAReadLEData;
+ connHandler.terminate = CATerminateLE;
+ registerCallback(connHandler, CA_ADAPTER_GATT_BTLE);
+
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
+
+ return CA_STATUS_OK;
+}
+
+CAResult_t CAStartLE()
+{
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE, not implemented");
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
+ return CA_STATUS_OK;
+}
+
+CAResult_t CAStopLE()
+{
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
+#ifndef SINGLE_THREAD
+ CAStopLEQueues();
+#endif
+
+ ca_mutex_lock(g_bleIsServerMutex);
+ if (true == g_isServer)
{
- length = totalLength;
- memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
+ CAStopLEGattServer();
}
else
{
- length = CA_SUPPORTED_BLE_MTU_SIZE;
- memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
- CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
+ CAStopLEGattClient();
}
+ ca_mutex_unlock(g_bleIsServerMutex);
- int32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
- int32_t index = 0;
- // Send the first segment with the header.
- if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
- {
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
- result = CAUpdateCharacteristicsToGattClient(
- bleData->remoteEndpoint->addr, dataSegment, length);
- if (CA_STATUS_OK != result)
- {
- OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
- g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
- OICFree(dataSegment);
- return;
- }
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
- for (index = 1; index < iter; index++)
- {
- // Send the remaining header.
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
- result = CAUpdateCharacteristicsToGattClient(
- bleData->remoteEndpoint->addr,
- bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
- CA_SUPPORTED_BLE_MTU_SIZE);
- if (CA_STATUS_OK != result)
- {
- OIC_LOG_V(ERROR, CALEADAPTER_TAG,
- "Update characteristics failed, result [%d]", result);
- g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
- OICFree(dataSegment);
- return;
- }
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
- CA_SUPPORTED_BLE_MTU_SIZE);
- }
+ return CA_STATUS_OK;
+}
- int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
- if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
- {
- // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
- result = CAUpdateCharacteristicsToGattClient(
- bleData->remoteEndpoint->addr,
- bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
- remainingLen);
- if (CA_STATUS_OK != result)
- {
- OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
- result);
- g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
- OICFree(dataSegment);
- return;
- }
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
- }
- }
+void CATerminateLE()
+{
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
+
+ CASetLEReqRespServerCallback(NULL);
+ CASetLEReqRespClientCallback(NULL);
+ CALERegisterNetworkNotifications(NULL);
+ CASetLEReqRespAdapterCallback(NULL);
+ CATerminateLENetworkMonitor();
+
+ ca_mutex_lock(g_bleIsServerMutex);
+ if (true == g_isServer)
+ {
+ CATerminateLEGattServer();
+ }
else
{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
- result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
- if (CA_STATUS_OK != result)
- {
- OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
- result);
- g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
- OICFree(dataSegment);
- return;
- }
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
- for (index = 1; index < iter; index++)
- {
- // Send the remaining header.
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
- result = CAUpdateCharacteristicsToAllGattClients(
- bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
- CA_SUPPORTED_BLE_MTU_SIZE);
- if (CA_STATUS_OK != result)
- {
- OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
- result);
- g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
- OICFree(dataSegment);
- return;
- }
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", CA_SUPPORTED_BLE_MTU_SIZE);
- }
-
- int32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
- if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
- {
- // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
- result = CAUpdateCharacteristicsToAllGattClients(
- bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
- remainingLen);
- if (CA_STATUS_OK != result)
- {
- OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
- result);
- g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
- OICFree(dataSegment);
- return;
- }
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
- }
+ CATerminateLEGattClient();
}
- OICFree(dataSegment);
+ ca_mutex_unlock(g_bleIsServerMutex);
+
+#ifndef SINGLE_THREAD
+ CATerminateLEQueues();
+#endif
+ CATerminateLEAdapterMutex();
OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
}
-void CABLEClientSendDataThread(void *threadData)
+CAResult_t CAStartLEListeningServer()
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
-
- CALEData_t *bleData = (CALEData_t *) threadData;
- if (!bleData)
+ CAResult_t result = CA_STATUS_OK;
+#ifndef SINGLE_THREAD
+ result = CAInitLEServerQueues();
+ if (CA_STATUS_OK != result)
{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
- return;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerQueues failed");
+ return CA_STATUS_FAILED;
}
+#endif
- char *header = (char *) OICCalloc(CA_HEADER_LENGTH, sizeof(char));
- VERIFY_NON_NULL_VOID(header, CALEADAPTER_TAG, "Malloc failed");
-
- uint32_t totalLength = bleData->dataLen + CA_HEADER_LENGTH;
- char *dataSegment = (char *) OICCalloc(totalLength + 1, sizeof(char));
- if (NULL == dataSegment)
+ result = CAGetLEAdapterState();
+ if (CA_ADAPTER_NOT_ENABLED == result)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failed");
- OICFree(header);
- return;
+ gLeServerStatus = CA_LISTENING_SERVER;
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
+ return CA_STATUS_OK;
}
- CAResult_t result = CAGenerateHeader(header, bleData->dataLen);
- if (CA_STATUS_OK != result )
+ if (CA_STATUS_FAILED == result)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
- OICFree(header);
- OICFree(dataSegment);
- return ;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
+ return CA_STATUS_FAILED;
}
- memcpy(dataSegment, header, CA_HEADER_LENGTH);
- OICFree(header);
- uint32_t length = 0;
- if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
+ CAStartLEGattServer();
+
+ ca_mutex_lock(g_bleIsServerMutex);
+ g_isServer = true;
+ ca_mutex_unlock(g_bleIsServerMutex);
+
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
+ return CA_STATUS_OK;
+}
+
+CAResult_t CAStartLEDiscoveryServer()
+{
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
+ CAResult_t result = CA_STATUS_OK;
+#ifndef SINGLE_THREAD
+ result = CAInitLEClientQueues();
+ if (CA_STATUS_OK != result)
{
- length = totalLength;
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
- memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientQueues failed");
+ return CA_STATUS_FAILED;
}
- else
+#endif
+ result = CAGetLEAdapterState();
+ if (CA_ADAPTER_NOT_ENABLED == result)
{
- length = CA_SUPPORTED_BLE_MTU_SIZE;
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "length [%d]", length);
- memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
- CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
+ gLeServerStatus = CA_DISCOVERY_SERVER;
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Listen Server will be started once BT Adapter is enabled");
+ return CA_STATUS_OK;
}
- uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
- uint32_t index = 0;
- if (NULL != bleData->remoteEndpoint) //Sending Unicast Data
+ if (CA_STATUS_FAILED == result)
{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Unicast Data");
- // Send the first segment with the header.
- result = CAUpdateCharacteristicsToGattServer(bleData->remoteEndpoint->addr,
- dataSegment,
- length,
- LE_UNICAST, 0);
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Bluetooth get state failed!");
+ return CA_STATUS_FAILED;
+ }
- if (CA_STATUS_OK != result)
- {
- OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", result);
- g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
- OICFree(dataSegment);
- return ;
- }
+ CAStartLEGattClient();
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
- for (index = 1; index < iter; index++)
- {
- // Send the remaining header.
- result = CAUpdateCharacteristicsToGattServer(
- bleData->remoteEndpoint->addr,
- bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
- CA_SUPPORTED_BLE_MTU_SIZE,
- LE_UNICAST, 0);
- if (CA_STATUS_OK != result)
- {
- OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
- result);
- g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
- OICFree(dataSegment);
- return;
- }
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
- CA_SUPPORTED_BLE_MTU_SIZE);
- }
+ ca_mutex_lock(g_bleIsServerMutex);
+ g_isServer = false;
+ ca_mutex_unlock(g_bleIsServerMutex);
- uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
- if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
- {
- // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
- result = CAUpdateCharacteristicsToGattServer(
- bleData->remoteEndpoint->addr,
- bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
- remainingLen,
- LE_UNICAST, 0);
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
+ return CA_STATUS_OK;
+}
- if (CA_STATUS_OK != result)
- {
- OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
- result);
- g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
- OICFree(dataSegment);
- return;
+CAResult_t CAStartLENotifyServer()
+{
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
+
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
+ return CA_STATUS_OK;
+}
+
+uint32_t CASendLENotification(const CAEndpoint_t *endpoint, const void *data,
+ uint32_t dataLen)
+{
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
+
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
+ return 0;
+}
+
+CAResult_t CAReadLEData()
+{
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
+#ifdef SINGLE_THREAD
+ CACheckLEData();
+#endif
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
+ return CA_STATUS_OK;
+}
+
+int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
+{
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
+
+ //Input validation
+ VERIFY_NON_NULL_RET(endpoint, NULL, "Remote endpoint is null", -1);
+ VERIFY_NON_NULL_RET(data, NULL, "Data is null", -1);
+
+ CAResult_t result = CA_STATUS_FAILED;
+
+ ca_mutex_lock(g_bleIsServerMutex);
+ if (true == g_isServer)
+ {
+ result = CALEAdapterServerSendData(endpoint, data, dataLen);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed\n");
+ if (g_errorHandler)
+ {
+ g_errorHandler((void *) endpoint, (void *) data, dataLen, result);
}
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
+ ca_mutex_unlock(g_bleIsServerMutex);
+ return -1;
}
}
else
{
- //Sending Mulitcast Data
- // Send the first segment with the header.
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data");
- result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length);
+ result = CALEAdapterClientSendData(endpoint, data, dataLen);
if (CA_STATUS_OK != result)
{
- OIC_LOG_V(ERROR, CALEADAPTER_TAG,
- "Update characteristics (all) failed, result [%d]", result);
- g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
- OICFree(dataSegment);
- return ;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data failed \n");
+ if (g_errorHandler)
+ {
+ g_errorHandler(endpoint, data, dataLen, result);
+ }
+ ca_mutex_unlock(g_bleIsServerMutex);
+ return -1;
}
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length);
- // Send the remaining header.
- for (index = 1; index < iter; index++)
+ }
+ ca_mutex_unlock(g_bleIsServerMutex);
+
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
+ return dataLen;
+}
+
+int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen)
+{
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
+
+ //Input validation
+ VERIFY_NON_NULL_RET(data, NULL, "Data is null", -1);
+
+ if (0 >= dataLen)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid Parameter");
+ return -1;
+ }
+
+ CAResult_t result = CA_STATUS_FAILED;
+
+ ca_mutex_lock(g_bleIsServerMutex);
+ if (true == g_isServer)
+ {
+ result = CALEAdapterServerSendData(NULL, data, dataLen);
+ if (CA_STATUS_OK != result)
{
- result = CAUpdateCharacteristicsToAllGattServers(
- bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
- CA_SUPPORTED_BLE_MTU_SIZE);
- if (CA_STATUS_OK != result)
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data failed" );
+
+ ca_mutex_unlock(g_bleIsServerMutex);
+ if (g_errorHandler)
{
- OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics (all) failed, result [%d]",
- result);
- g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
- OICFree(dataSegment);
- return;
+ g_errorHandler(endpoint, data, dataLen, result);
}
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]",
- CA_SUPPORTED_BLE_MTU_SIZE);
+ return -1;
}
-
- uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
- if ( remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
+ }
+ else
+ {
+ result = CALEAdapterClientSendData(NULL, data, dataLen);
+ if (CA_STATUS_OK != result)
{
- // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
- result = CAUpdateCharacteristicsToAllGattServers(
- bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
- remainingLen);
- if (CA_STATUS_OK != result)
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data failed" );
+ if (g_errorHandler)
{
- OIC_LOG_V(ERROR, CALEADAPTER_TAG,
- "Update characteristics (all) failed, result [%d]", result);
- g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
- OICFree(dataSegment);
- return;
+ g_errorHandler(endpoint, data, dataLen, result);
}
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", remainingLen);
+ ca_mutex_unlock(g_bleIsServerMutex);
+ return -1;
}
-
}
+ ca_mutex_unlock(g_bleIsServerMutex);
- OICFree(dataSegment);
-
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread");
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
+ return dataLen;
}
-CALEData_t *CACreateBLEData(const CAEndpoint_t *remoteEndpoint, const void *data,
- uint32_t dataLength)
+CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
{
- CALEData_t *bleData = (CALEData_t *) OICMalloc(sizeof(CALEData_t));
- if (!bleData)
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
+
+ VERIFY_NON_NULL(info, NULL, "CALocalConnectivity info is null");
+
+ char *local_address = NULL;
+
+ CAResult_t res = CAGetLEAddress(&local_address);
+ if (CA_STATUS_OK != res)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
- return NULL;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "CAGetLEAddress has failed");
+ return res;
}
- bleData->remoteEndpoint = CAAdapterCloneEndpoint(remoteEndpoint);
- bleData->data = (void *)OICCalloc(dataLength + 1, 1);
- if (NULL == bleData->data)
+ if (NULL == local_address)
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
- CAFreeBLEData(bleData);
- return NULL;
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is NULL");
+ return CA_STATUS_FAILED;
}
- memcpy(bleData->data, data, dataLength);
- bleData->dataLen = dataLength;
- return bleData;
+ *size = 0;
+ (*info) = (CAEndpoint_t *) OICCalloc(1, sizeof(CAEndpoint_t));
+ if (NULL == (*info))
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Malloc failure!");
+ OICFree(local_address);
+ return CA_STATUS_FAILED;
+ }
+
+ size_t local_address_len = strlen(local_address);
+
+ if(local_address_len >= sizeof(g_localBLEAddress) ||
+ local_address_len >= MAX_ADDR_STR_SIZE_CA - 1)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "local_address is too long");
+ OICFree(*info);
+ OICFree(local_address);
+ return CA_STATUS_FAILED;
+ }
+
+ OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address);
+ ca_mutex_lock(g_bleLocalAddressMutex);
+ OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address);
+ ca_mutex_unlock(g_bleLocalAddressMutex);
+
+ (*info)->adapter = CA_ADAPTER_GATT_BTLE;
+ *size = 1;
+ OICFree(local_address);
+
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
+ return CA_STATUS_OK;
}
-void CAFreeBLEData(CALEData_t *bleData)
+CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCallback)
{
- VERIFY_NON_NULL_VOID(bleData, NULL, "Param bleData is NULL");
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
- CAAdapterFreeEndpoint(bleData->remoteEndpoint);
- OICFree(bleData->data);
- OICFree(bleData);
+ ca_mutex_lock(g_bleNetworkCbMutex);
+ g_networkCallback = netCallback;
+ ca_mutex_unlock(g_bleNetworkCbMutex);
+ CAResult_t res = CA_STATUS_OK;
+ if (netCallback)
+ {
+ res = CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCb);
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
+ }
+ }
+ else
+ {
+ res = CAUnSetLEAdapterStateChangedCb();
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
+ }
+ }
+
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
+ return res;
}
-void CALEDataDestroyer(void *data, uint32_t size)
+void CALEDeviceStateChangedCb( CAAdapterState_t adapter_state)
{
- CALEData_t *ledata = (CALEData_t *) data;
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
- CAFreeBLEData(ledata);
-}
+ VERIFY_NON_NULL_VOID(g_localBLEAddress, NULL, "g_localBLEAddress is null");
+ CAEndpoint_t localEndpoint = {};
+
+ ca_mutex_lock(g_bleLocalAddressMutex);
+ OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), g_localBLEAddress);
+ ca_mutex_unlock(g_bleLocalAddressMutex);
+
+ g_bleAdapterState = adapter_state;
+ // Start a GattServer/Client if gLeServerStatus is SET
+ if (CA_LISTENING_SERVER == gLeServerStatus)
+ {
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer");
+ CAStartLEGattServer();
+ }
+ else if (CA_DISCOVERY_SERVER == gLeServerStatus)
+ {
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartBleGattClient");
+ CAStartLEGattClient();
+ }
+ gLeServerStatus = CA_SERVER_NOTSTARTED;
+
+ ca_mutex_lock(g_bleNetworkCbMutex);
+ if (NULL != g_networkCallback)
+ {
+ g_networkCallback(&localEndpoint, adapter_state);
+ }
+ else
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL");
+ }
+ ca_mutex_unlock(g_bleNetworkCbMutex);
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
+}
-CAResult_t CABLEClientSendData(const CAEndpoint_t *remoteEndpoint,
- const void *data,
- uint32_t dataLen)
+CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint,
+ const void *data,
+ uint32_t dataLen)
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
VERIFY_NON_NULL(data, NULL, "Param data is NULL");
-
+#ifndef SINGLE_THREAD
VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
"g_bleClientSendQueueHandle is NULL",
CA_STATUS_FAILED);
OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
- CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
+ CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
if (!bleData)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
ca_mutex_lock(g_bleClientSendDataMutex);
CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
ca_mutex_unlock(g_bleClientSendDataMutex);
-
+#endif
OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
return CA_STATUS_OK;
}
-CAResult_t CABLEServerSendData(const CAEndpoint_t *remoteEndpoint,
- const void *data,
- uint32_t dataLen)
+CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint,
+ const void *data,
+ uint32_t dataLen)
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
VERIFY_NON_NULL(data, NULL, "Param data is NULL");
- VERIFY_NON_NULL_RET(g_sendQueueHandle, CALEADAPTER_TAG,
+#ifdef SINGLE_THREAD
+ char header[CA_HEADER_LENGTH] = {0};
+
+ CAResult_t result = CAGenerateHeader(header, dataLen);
+
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
+ return -1;
+ }
+
+ if (!CAIsLEConnected())
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
+ return -1;
+ }
+
+ result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
+ return -1;
+ }
+
+ int32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
+ for (int32_t iter = 0; iter < dataLimit; iter++)
+ {
+ result = CAUpdateCharacteristicsToAllGattClients((data +
+ (iter * CA_SUPPORTED_BLE_MTU_SIZE)),
+ CA_SUPPORTED_BLE_MTU_SIZE);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
+ return -1;
+ }
+ CALEDoEvents();
+ }
+
+ uint8_t remainingLen = dataLen % CA_SUPPORTED_BLE_MTU_SIZE;
+ if(remainingLen)
+ {
+ result = CAUpdateCharacteristicsToAllGattClients((data +
+ (dataLimit * CA_SUPPORTED_BLE_MTU_SIZE)),
+ remainingLen);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
+ return -1;
+ }
+ CALEDoEvents();
+ }
+#else
+ VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG,
"BleClientReceiverQueue is NULL",
CA_STATUS_FAILED);
VERIFY_NON_NULL_RET(g_bleServerSendDataMutex, CALEADAPTER_TAG,
"BleClientSendDataMutex is NULL",
CA_STATUS_FAILED);
- VERIFY_NON_NULL_RET(g_sendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
+ VERIFY_NON_NULL_RET(g_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
CA_STATUS_FAILED);
OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
- CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLen);
+ CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen);
if (!bleData)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
}
// Add message to send queue
ca_mutex_lock(g_bleServerSendDataMutex);
- CAQueueingThreadAddData(g_sendQueueHandle, bleData, sizeof(CALEData_t));
+ CAQueueingThreadAddData(g_bleServerSendQueueHandle, bleData, sizeof(CALEData_t));
ca_mutex_unlock(g_bleServerSendDataMutex);
-
+#endif
OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
return CA_STATUS_OK;
}
-CAResult_t CABLEServerReceivedData(const char *remoteAddress, const char *serviceUUID,
- const void *data, uint32_t dataLength, uint32_t *sentLength)
+CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress, const char *serviceUUID,
+ const void *data, uint32_t dataLength,
+ uint32_t *sentLength)
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
+
+#ifdef SINGLE_THREAD
+ if(g_networkPacketReceivedCallback)
+ {
+ CAEndpoint_t endPoint = { 0 }; // will be filled by upper layer
+ endPoint.adapter = CA_ADAPTER_GATT_BTLE;
+ g_networkPacketReceivedCallback(&endPoint, data, dataLength);
+ }
+#else
VERIFY_NON_NULL_RET(g_bleServerReceiverQueue, CALEADAPTER_TAG, "g_bleServerReceiverQueue",
CA_STATUS_FAILED);
//Add message to data queue
- CAEndpoint_t *remoteEndpoint = CAAdapterCreateEndpoint(0, CA_ADAPTER_GATT_BTLE, remoteAddress, 0);
+ CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
+ CA_ADAPTER_GATT_BTLE,
+ remoteAddress, 0);
if (NULL == remoteEndpoint)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
// Create bleData to add to queue
OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
- CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
+ CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
if (!bleData)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
- CAAdapterFreeEndpoint(remoteEndpoint);
+ CAFreeEndpoint(remoteEndpoint);
return CA_MEMORY_ALLOC_FAILED;
}
- CAAdapterFreeEndpoint(remoteEndpoint);
+ CAFreeEndpoint(remoteEndpoint);
// Add message to send queue
CAQueueingThreadAddData(g_bleServerReceiverQueue, bleData, sizeof(CALEData_t));
*sentLength = dataLength;
-
+#endif
OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
return CA_STATUS_OK;
}
-CAResult_t CABLEClientReceivedData(const char *remoteAddress, const char *serviceUUID,
- const void *data, uint32_t dataLength, uint32_t *sentLength)
+CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress, const char *serviceUUID,
+ const void *data, uint32_t dataLength,
+ uint32_t *sentLength)
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
VERIFY_NON_NULL(serviceUUID, CALEADAPTER_TAG, "service UUID is null");
VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null");
VERIFY_NON_NULL(sentLength, CALEADAPTER_TAG, "Sent data length holder is null");
+#ifndef SINGLE_THREAD
VERIFY_NON_NULL_RET(g_bleClientReceiverQueue, CALEADAPTER_TAG, "g_bleClientReceiverQueue",
CA_STATUS_FAILED);
//Add message to data queue
- CAEndpoint_t *remoteEndpoint = CAAdapterCreateEndpoint(0, CA_ADAPTER_GATT_BTLE, remoteAddress, 0);
+ CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS,
+ CA_ADAPTER_GATT_BTLE,
+ remoteAddress, 0);
if (NULL == remoteEndpoint)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%d]", dataLength);
// Create bleData to add to queue
- CALEData_t *bleData = CACreateBLEData(remoteEndpoint, data, dataLength);
+ CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLength);
if (!bleData)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!");
- CAAdapterFreeEndpoint(remoteEndpoint);
+ CAFreeEndpoint(remoteEndpoint);
return CA_MEMORY_ALLOC_FAILED;
}
- CAAdapterFreeEndpoint(remoteEndpoint);
+ CAFreeEndpoint(remoteEndpoint);
// Add message to send queue
CAQueueingThreadAddData(g_bleClientReceiverQueue, bleData, sizeof(CALEData_t));
*sentLength = dataLength;
-
+#endif
OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
return CA_STATUS_OK;
}
-void CASetBleAdapterThreadPoolHandle(ca_thread_pool_t handle)
+void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
}
-void CASetBLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
+void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN");
OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN");
VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null");
- CAEndpoint_t *rep = OICCalloc(1, sizeof(CAEndpoint_t));
-
- if (!rep)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create remote endpoint !");
- return;
- }
-
- if (remoteAddress)
- {
- OICStrcpy(rep->addr, sizeof(rep->addr), remoteAddress);
- }
-
- rep->adapter = CA_ADAPTER_GATT_BTLE;
- rep->flags = CA_DEFAULT_FLAGS;
-
+ CAEndpoint_t *rep = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_GATT_BTLE,
+ remoteAddress, 0);
//if required, will be used to build remote end point
g_errorHandler(rep, data, dataLen, result);
- CAAdapterFreeEndpoint(rep);
+ CAFreeEndpoint(rep);
OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");
}