X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fcsdk%2Fconnectivity%2Fsrc%2Fbt_le_adapter%2Fcaleadapter.c;h=3176f83c73ffa3550ed52041d64f5e80ff8a5e73;hb=refs%2Ftags%2Fsubmit%2Ftizen_4.0%2F20171010.021147;hp=a32949ed2754dbf075fe147a398af1dc320c2557;hpb=cb25caa720e89e42d77e7190664ce7fba6339e8d;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c b/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c old mode 100644 new mode 100755 index a32949e..3176f83 --- a/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c @@ -26,8 +26,11 @@ #include "caleinterface.h" #include "cacommon.h" -#include "camutex.h" +#include "octhread.h" #include "caadapterutils.h" +#ifdef __WITH_DTLS__ +#include "ca_adapter_net_ssl.h" +#endif #ifndef SINGLE_THREAD #include "caqueueingthread.h" #endif @@ -38,7 +41,7 @@ #include "oic_malloc.h" #include "oic_string.h" #include "caremotehandler.h" -#include "pdu.h" +#include /** * Logging tag for module name. @@ -48,8 +51,7 @@ /** * Stores information of all the senders. * - * This structure will be used to track and defragment all incoming - * data packet. + * This structure will be used to track and defragment all incoming data packet. */ typedef struct { @@ -68,6 +70,11 @@ typedef enum } CABLEAdapter_t; /** + * mtu size to use in fragmentation logic. + * default value is 20 byte. + */ +static uint16_t g_mtuSize = CA_DEFAULT_BLE_MTU_SIZE; +/** * Callback to provide the status of the network change to CA layer. */ static CAAdapterChangeCallback g_networkCallback = NULL; @@ -93,23 +100,27 @@ static char g_localBLEAddress[18] = { 0 }; */ static CABLEAdapter_t g_adapterType = ADAPTER_EMPTY; +#ifdef __WITH_DTLS__ +static CADataType_t g_dataType = CA_REQUEST_DATA; +#endif + /** * Mutex to synchronize the task to be executed on the GattServer * function calls. */ -static ca_mutex g_bleIsServerMutex = NULL; +static oc_mutex g_bleIsServerMutex = NULL; /** * Mutex to synchronize the callback to be called for the network * changes. */ -static ca_mutex g_bleNetworkCbMutex = NULL; +static oc_mutex g_bleNetworkCbMutex = NULL; /** * Mutex to synchronize the updates of the local LE address of the * adapter. */ -static ca_mutex g_bleLocalAddressMutex = NULL; +static oc_mutex g_bleLocalAddressMutex = NULL; /** * Reference to thread pool. @@ -119,28 +130,33 @@ static ca_thread_pool_t g_bleAdapterThreadPool = NULL; /** * Mutex to synchronize the task to be pushed to thread pool. */ -static ca_mutex g_bleAdapterThreadPoolMutex = NULL; +static oc_mutex g_bleAdapterThreadPoolMutex = NULL; /** * Mutex to synchronize the queing of the data from SenderQueue. */ -static ca_mutex g_bleClientSendDataMutex = NULL; +static oc_mutex g_bleClientSendDataMutex = NULL; /** * Mutex to synchronize the queing of the data from ReceiverQueue. */ -static ca_mutex g_bleReceiveDataMutex = NULL; +static oc_mutex g_bleClientReceiveDataMutex = NULL; /** * Mutex to synchronize the queing of the data from SenderQueue. */ -static ca_mutex g_bleServerSendDataMutex = NULL; +static oc_mutex g_bleServerSendDataMutex = NULL; + +/** + * Mutex to synchronize the queing of the data from ReceiverQueue. + */ +static oc_mutex g_bleServerReceiveDataMutex = NULL; /** * Mutex to synchronize the callback to be called for the * adapterReqResponse. */ -static ca_mutex g_bleAdapterReqRespCbMutex = NULL; +static oc_mutex g_bleAdapterReqRespCbMutex = NULL; /** * Callback to be called when network packet received from either @@ -153,6 +169,14 @@ static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL; */ static CAErrorHandleCallback g_errorHandler = NULL; +#ifdef __WITH_DTLS__ +static void CALESecureReceiveDataCB(const CASecureEndpoint_t *endpoint, + const void *data, size_t dataLength); + +static ssize_t CALESecureSendDataCB(CAEndpoint_t *endpoint, + const void *data, size_t dataLength); +#endif + #ifdef SINGLE_THREAD /** * Pointer to defragment received data from single threaded routine. @@ -263,31 +287,44 @@ static void CALEErrorHandler(const char *remoteAddress, #ifndef SINGLE_THREAD /** - * Stop condition of recvhandler. + * Stop condition of Server recvhandler. */ -static bool g_dataBleReceiverHandlerState = false; +static bool g_dataBleServerReceiverHandlerState = false; /** - * Sender information. + * Stop condition of Client recvhandler. + */ +static bool g_dataBleClientReceiverHandlerState = false; + +/** + * Sender information of Server. */ static u_arraylist_t *g_bleServerSenderInfo = NULL; +/** + * Sender information of Client. + */ static u_arraylist_t *g_bleClientSenderInfo = NULL; /** + * Queue to process the outgoing packets from GATTServer. + */ +static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL; + +/** * Queue to process the outgoing packets from GATTClient. */ static CAQueueingThread_t *g_bleClientSendQueueHandle = NULL; /** - * Queue to process the incoming packets. + * Queue to process the incoming packets from GATTServer. */ -static CAQueueingThread_t *g_bleReceiverQueue = NULL; +static CAQueueingThread_t *g_bleServerReceiverQueue = NULL; /** - * Queue to process the outgoing packets from GATTServer. + * Queue to process the incoming packets from GATTClient. */ -static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL; +static CAQueueingThread_t *g_bleClientReceiverQueue = NULL; /** * This function will be associated with the sender queue for @@ -316,16 +353,39 @@ static void CALEServerSendDataThread(void *threadData); static void CALEClientSendDataThread(void *threadData); /** - * This function will be associated with the receiver queue. - * * This function will defragment the received data from each sender * respectively and will send it up to CA layer. Respective sender's * header will provide the length of the data sent. * + * @param[in] threadData Data pushed to the queue which contains the + * info about RemoteEndpoint and Data. + * @param[in] receiverType Whether receiver is server or client. + */ +static void CALEDataReceiverHandler(void *threadData, CABLEAdapter_t receiverType); + +/** + * This function will be associated with the receiver queue for + * GattServer. + * + * This function will call the function CALEDataReceiverHandler() + * with server type to defragment the received data. + * * @param[in] threadData Data pushed to the queue which contains the * info about RemoteEndpoint and Data. */ -static void CALEDataReceiverHandler(void *threadData); +static void CALEServerDataReceiverHandler(void *threadData); + +/** + * This function will be associated with the receiver queue for + * GattClient. + * + * This function will call the function CALEDataReceiverHandler() + * with client type to defragment the received data. + * + * @param[in] threadData Data pushed to the queue which contains the + * info about RemoteEndpoint and Data. + */ +static void CALEClientDataReceiverHandler(void *threadData); /** * This function will stop all queues created for GattServer and @@ -396,8 +456,22 @@ static CAResult_t CAInitLEClientSenderQueue(); /** * This function will initialize the Receiver queue for - * LEAdapter. This will initialize the queue to process the function - * CABLEDataReceiverHandler() when ever the task is added to this + * GattServer. This will initialize the queue to process the function + * CALEServerDataReceiverHandler() when ever the task is added to this + * queue. + * + * @return ::CA_STATUS_OK or Appropriate error code + * @retval ::CA_STATUS_OK Successful + * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments + * @retval ::CA_STATUS_FAILED Operation failed + * + */ +static CAResult_t CAInitLEServerReceiverQueue(); + +/** + * This function will initialize the Receiver queue for + * GattClient. This will initialize the queue to process the function + * CALEClientDataReceiverHandler() when ever the task is added to this * queue. * * @return ::CA_STATUS_OK or Appropriate error code @@ -406,7 +480,7 @@ static CAResult_t CAInitLEClientSenderQueue(); * @retval ::CA_STATUS_FAILED Operation failed * */ -static CAResult_t CAInitLEReceiverQueue(); +static CAResult_t CAInitLEClientReceiverQueue(); /** * This function will create the Data required to send it in the @@ -449,7 +523,7 @@ static void CALEDataDestroyer(void *data, uint32_t size); * @param[in] address target address to remove data in queue. */ static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle, - ca_mutex mutex, + oc_mutex mutex, const char* address); /** @@ -495,15 +569,13 @@ static CAResult_t CALEGetPortsFromSenderInfo(const char *leAddress, static CAResult_t CAInitLEServerQueues() { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN"); - - ca_mutex_lock(g_bleAdapterThreadPoolMutex); + oc_mutex_lock(g_bleAdapterThreadPoolMutex); CAResult_t result = CAInitLEServerSenderQueue(); if (CA_STATUS_OK != result) { OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed"); - ca_mutex_unlock(g_bleAdapterThreadPoolMutex); + oc_mutex_unlock(g_bleAdapterThreadPoolMutex); return CA_STATUS_FAILED; } @@ -511,38 +583,34 @@ static CAResult_t CAInitLEServerQueues() if (!g_bleServerSenderInfo) { OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!"); - ca_mutex_unlock(g_bleAdapterThreadPoolMutex); + oc_mutex_unlock(g_bleAdapterThreadPoolMutex); return CA_MEMORY_ALLOC_FAILED; } - result = CAInitLEReceiverQueue(); + result = CAInitLEServerReceiverQueue(); if (CA_STATUS_OK != result) { - OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed"); + OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerReceiverQueue failed"); u_arraylist_free(&g_bleServerSenderInfo); - ca_mutex_unlock(g_bleAdapterThreadPoolMutex); + oc_mutex_unlock(g_bleAdapterThreadPoolMutex); return CA_STATUS_FAILED; } - g_dataBleReceiverHandlerState = true; + g_dataBleServerReceiverHandlerState = true; - ca_mutex_unlock(g_bleAdapterThreadPoolMutex); - - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); + oc_mutex_unlock(g_bleAdapterThreadPoolMutex); return CA_STATUS_OK; } static CAResult_t CAInitLEClientQueues() { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN"); - - ca_mutex_lock(g_bleAdapterThreadPoolMutex); + oc_mutex_lock(g_bleAdapterThreadPoolMutex); CAResult_t result = CAInitLEClientSenderQueue(); if (CA_STATUS_OK != result) { OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed"); - ca_mutex_unlock(g_bleAdapterThreadPoolMutex); + oc_mutex_unlock(g_bleAdapterThreadPoolMutex); return CA_STATUS_FAILED; } @@ -550,71 +618,108 @@ static CAResult_t CAInitLEClientQueues() if (!g_bleClientSenderInfo) { OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!"); - ca_mutex_unlock(g_bleAdapterThreadPoolMutex); + oc_mutex_unlock(g_bleAdapterThreadPoolMutex); return CA_MEMORY_ALLOC_FAILED; } - result = CAInitLEReceiverQueue(); + result = CAInitLEClientReceiverQueue(); if (CA_STATUS_OK != result) { - OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEReceiverQueue failed"); + OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientReceiverQueue failed"); u_arraylist_free(&g_bleClientSenderInfo); - ca_mutex_unlock(g_bleAdapterThreadPoolMutex); + oc_mutex_unlock(g_bleAdapterThreadPoolMutex); return CA_STATUS_FAILED; } - g_dataBleReceiverHandlerState = true; + g_dataBleClientReceiverHandlerState = true; + + oc_mutex_unlock(g_bleAdapterThreadPoolMutex); + return CA_STATUS_OK; +} + +static CAResult_t CAInitLEServerReceiverQueue() +{ + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__); + // Check if the message queue is already initialized + if (g_bleServerReceiverQueue) + { + OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!"); + return CA_STATUS_OK; + } + + // Create recv message queue + g_bleServerReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t)); + if (!g_bleServerReceiverQueue) + { + OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!"); + return CA_MEMORY_ALLOC_FAILED; + } - ca_mutex_unlock(g_bleAdapterThreadPoolMutex); + if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleServerReceiverQueue, + g_bleAdapterThreadPool, + CALEServerDataReceiverHandler, + CALEDataDestroyer)) + { + OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize server receiver 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 CA_STATUS_OK; } -static CAResult_t CAInitLEReceiverQueue() +static CAResult_t CAInitLEClientReceiverQueue() { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEReceiverQueue"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__); // Check if the message queue is already initialized - if (g_bleReceiverQueue) + if (g_bleClientReceiverQueue) { OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!"); return CA_STATUS_OK; } // Create recv message queue - g_bleReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t)); - if (!g_bleReceiverQueue) + g_bleClientReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t)); + if (!g_bleClientReceiverQueue) { OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!"); return CA_MEMORY_ALLOC_FAILED; } - if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleReceiverQueue, + if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientReceiverQueue, g_bleAdapterThreadPool, - CALEDataReceiverHandler, + CALEClientDataReceiverHandler, CALEDataDestroyer)) { - OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread"); - OICFree(g_bleReceiverQueue); - g_bleReceiverQueue = NULL; + OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize client receiver queue thread"); + OICFree(g_bleClientReceiverQueue); + g_bleClientReceiverQueue = NULL; return CA_STATUS_FAILED; } - if (CA_STATUS_OK != CAQueueingThreadStart(g_bleReceiverQueue)) + if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientReceiverQueue)) { OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed "); - OICFree(g_bleReceiverQueue); - g_bleReceiverQueue = NULL; + OICFree(g_bleClientReceiverQueue); + g_bleClientReceiverQueue = NULL; return CA_STATUS_FAILED; } - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); return CA_STATUS_OK; } static CAResult_t CAInitLEServerSenderQueue() { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEServerSenderQueue"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__); // Check if the message queue is already initialized if (g_bleServerSendQueueHandle) { @@ -641,7 +746,6 @@ static CAResult_t CAInitLEServerSenderQueue() return CA_STATUS_FAILED; } - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); return CA_STATUS_OK; } @@ -670,7 +774,7 @@ static void CALEClearSenderInfo() static CAResult_t CAInitLEClientSenderQueue() { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEClientSenderQueue"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__); if (g_bleClientSendQueueHandle) { @@ -695,29 +799,32 @@ static CAResult_t CAInitLEClientSenderQueue() g_bleClientSendQueueHandle = NULL; return CA_STATUS_FAILED; } - - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAInitLEClientSenderQueue"); return CA_STATUS_OK; } static void CAStopLEQueues() { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStopLEQueues"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__); + + oc_mutex_lock(g_bleServerReceiveDataMutex); + if (NULL != g_bleServerReceiverQueue) + { + CAQueueingThreadStop(g_bleServerReceiverQueue); + } + oc_mutex_unlock(g_bleServerReceiveDataMutex); - ca_mutex_lock(g_bleReceiveDataMutex); - if (NULL != g_bleReceiverQueue) + oc_mutex_lock(g_bleClientReceiveDataMutex); + if (NULL != g_bleClientReceiverQueue) { - CAQueueingThreadStop(g_bleReceiverQueue); + CAQueueingThreadStop(g_bleClientReceiverQueue); } - ca_mutex_unlock(g_bleReceiveDataMutex); + oc_mutex_unlock(g_bleClientReceiveDataMutex); - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAStopLEQueues"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__); } static void CATerminateLEQueues() { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN"); - CAQueueingThreadDestroy(g_bleClientSendQueueHandle); OICFree(g_bleClientSendQueueHandle); g_bleClientSendQueueHandle = NULL; @@ -726,13 +833,15 @@ static void CATerminateLEQueues() OICFree(g_bleServerSendQueueHandle); g_bleServerSendQueueHandle = NULL; - CAQueueingThreadDestroy(g_bleReceiverQueue); - OICFree(g_bleReceiverQueue); - g_bleReceiverQueue = NULL; + CAQueueingThreadDestroy(g_bleServerReceiverQueue); + OICFree(g_bleServerReceiverQueue); + g_bleServerReceiverQueue = NULL; - CALEClearSenderInfo(); + CAQueueingThreadDestroy(g_bleClientReceiverQueue); + OICFree(g_bleClientReceiverQueue); + g_bleClientReceiverQueue = NULL; - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); + CALEClearSenderInfo(); } static CAResult_t CALEGetSenderInfo(const char *leAddress, @@ -777,35 +886,51 @@ static CAResult_t CALEGetSenderInfo(const char *leAddress, return CA_STATUS_FAILED; } -static void CALEDataReceiverHandler(void *threadData) +static void CALEDataReceiverHandler(void *threadData, CABLEAdapter_t receiverType) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDataReceiverHandler"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__); - ca_mutex_lock(g_bleReceiveDataMutex); + oc_mutex bleReceiveDataMutex = NULL; + bool dataBleReceiverHandlerState = false; - if (g_dataBleReceiverHandlerState) + switch (receiverType) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "checking for DE Fragmentation"); + case ADAPTER_CLIENT: + bleReceiveDataMutex = g_bleClientReceiveDataMutex; + dataBleReceiverHandlerState = g_dataBleClientReceiverHandlerState; + break; + case ADAPTER_SERVER: + bleReceiveDataMutex = g_bleServerReceiveDataMutex; + dataBleReceiverHandlerState = g_dataBleServerReceiverHandlerState; + break; + default: + OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Unsupported receiver type : %d", receiverType); + return; + } + + oc_mutex_lock(bleReceiveDataMutex); + if (dataBleReceiverHandlerState) + { CALEData_t *bleData = (CALEData_t *) threadData; if (!bleData) { OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!"); - ca_mutex_unlock(g_bleReceiveDataMutex); + oc_mutex_unlock(bleReceiveDataMutex); return; } if (!(bleData->senderInfo)) { OIC_LOG(ERROR, CALEADAPTER_TAG, "sender info is not available"); - ca_mutex_unlock(g_bleReceiveDataMutex); + oc_mutex_unlock(bleReceiveDataMutex); return; } if (!(bleData->remoteEndpoint)) { - OIC_LOG(ERROR, CALEADAPTER_TAG, "Client RemoteEndPoint NULL!!"); - ca_mutex_unlock(g_bleReceiveDataMutex); + OIC_LOG(ERROR, CALEADAPTER_TAG, "RemoteEndPoint NULL!!"); + oc_mutex_unlock(bleReceiveDataMutex); return; } @@ -819,7 +944,7 @@ static void CALEDataReceiverHandler(void *threadData) uint16_t destPort = 0; CAParseHeader(bleData->data, &startFlag, &sourcePort, &secureFlag, &destPort); - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, + OIC_LOG_V(INFO, CALEADAPTER_TAG, "header info: startFlag[%X] sourcePort[%d] secureFlag[%X] destPort[%d]", startFlag, sourcePort, secureFlag, destPort); @@ -828,7 +953,7 @@ static void CALEDataReceiverHandler(void *threadData) OIC_LOG_V(ERROR, CALEADAPTER_TAG, "this packet is not valid for this app(port mismatch[mine:%d, packet:%d])", g_localBLESourcePort, destPort); - ca_mutex_unlock(g_bleReceiveDataMutex); + oc_mutex_unlock(bleReceiveDataMutex); return; } @@ -866,7 +991,7 @@ static void CALEDataReceiverHandler(void *threadData) else { OIC_LOG(ERROR, CALEADAPTER_TAG, "This packet is wrong packet! ignore."); - ca_mutex_unlock(g_bleReceiveDataMutex); + oc_mutex_unlock(bleReceiveDataMutex); return; } @@ -874,7 +999,7 @@ static void CALEDataReceiverHandler(void *threadData) if (!newSender) { OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender"); - ca_mutex_unlock(g_bleReceiveDataMutex); + oc_mutex_unlock(bleReceiveDataMutex); return; } newSender->recvDataLen = 0; @@ -890,15 +1015,15 @@ static void CALEDataReceiverHandler(void *threadData) { OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!"); OICFree(newSender); - ca_mutex_unlock(g_bleReceiveDataMutex); + oc_mutex_unlock(bleReceiveDataMutex); return; } size_t dataOnlyLen = bleData->dataLen - (CA_BLE_HEADER_SIZE + CA_BLE_LENGTH_HEADER_SIZE); - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes", + OIC_LOG_V(INFO, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes", newSender->totalDataLen); - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes", + OIC_LOG_V(INFO, CALEADAPTER_TAG, "data received in the first packet [%zu] bytes", dataOnlyLen); newSender->defragData = OICCalloc(newSender->totalDataLen + 1, @@ -908,12 +1033,19 @@ static void CALEDataReceiverHandler(void *threadData) { OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!"); OICFree(newSender); - ca_mutex_unlock(g_bleReceiveDataMutex); + oc_mutex_unlock(bleReceiveDataMutex); return; } + CATransportFlags_t flags = CA_DEFAULT_FLAGS; +#ifdef __WITH_DTLS__ + if (CA_BLE_PACKET_SECURE == secureFlag) + { + flags |= CA_SECURE; + } +#endif const char *remoteAddress = bleData->remoteEndpoint->addr; - newSender->remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS, + newSender->remoteEndpoint = CACreateEndpointObject(flags, CA_ADAPTER_GATT_BTLE, remoteAddress, bleData->remoteEndpoint->port); @@ -923,7 +1055,7 @@ static void CALEDataReceiverHandler(void *threadData) OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!"); OICFree(newSender->defragData); OICFree(newSender); - ca_mutex_unlock(g_bleReceiveDataMutex); + oc_mutex_unlock(bleReceiveDataMutex); return; } @@ -933,7 +1065,7 @@ static void CALEDataReceiverHandler(void *threadData) OICFree(newSender->defragData); CAFreeEndpoint(newSender->remoteEndpoint); OICFree(newSender); - ca_mutex_unlock(g_bleReceiveDataMutex); + oc_mutex_unlock(bleReceiveDataMutex); return; } memcpy(newSender->defragData, @@ -954,7 +1086,7 @@ static void CALEDataReceiverHandler(void *threadData) OICFree(newSender->defragData); CAFreeEndpoint(newSender->remoteEndpoint); OICFree(newSender); - ca_mutex_unlock(g_bleReceiveDataMutex); + oc_mutex_unlock(bleReceiveDataMutex); return; } senderInfo = newSender; @@ -965,27 +1097,27 @@ static void CALEDataReceiverHandler(void *threadData) if (senderInfo->recvDataLen + dataOnlyLen > senderInfo->totalDataLen) { OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "Data Length exceeding error!! Receiving [%d] total length [%d]", + "Data Length exceeding error!! Receiving [%zu] total length [%u]", senderInfo->recvDataLen + dataOnlyLen, senderInfo->totalDataLen); u_arraylist_remove(bleData->senderInfo, senderIndex); OICFree(senderInfo->defragData); OICFree(senderInfo); - ca_mutex_unlock(g_bleReceiveDataMutex); + oc_mutex_unlock(bleReceiveDataMutex); return; } - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]", + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%zu]", dataOnlyLen); memcpy(senderInfo->defragData + senderInfo->recvDataLen, bleData->data + CA_BLE_HEADER_SIZE, dataOnlyLen); senderInfo->recvDataLen += dataOnlyLen; - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] received Datalen [%d]", - senderInfo->totalDataLen, senderInfo->recvDataLen); + OIC_LOG_V(INFO, CALEADAPTER_TAG, "totalDatalength [%d] received Datalen [%d]", + senderInfo->totalDataLen, senderInfo->recvDataLen); } if (senderInfo->totalDataLen == senderInfo->recvDataLen) { - ca_mutex_lock(g_bleAdapterReqRespCbMutex); + oc_mutex_lock(g_bleAdapterReqRespCbMutex); if (NULL == g_networkPacketReceivedCallback) { OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!"); @@ -993,43 +1125,109 @@ static void CALEDataReceiverHandler(void *threadData) u_arraylist_remove(bleData->senderInfo, senderIndex); OICFree(senderInfo->defragData); OICFree(senderInfo); - ca_mutex_unlock(g_bleAdapterReqRespCbMutex); - ca_mutex_unlock(g_bleReceiveDataMutex); + oc_mutex_unlock(g_bleAdapterReqRespCbMutex); + oc_mutex_unlock(bleReceiveDataMutex); return; } - OIC_LOG(DEBUG, CALEADAPTER_TAG, "[CALEDataReceiverHandler] Sending data up !"); + OIC_LOG(DEBUG, CALEADAPTER_TAG, "[CALEDataReceiverHandler] Received data up !"); const CASecureEndpoint_t tmp = { .endpoint = *senderInfo->remoteEndpoint }; - g_networkPacketReceivedCallback(&tmp, - senderInfo->defragData, - senderInfo->recvDataLen); - ca_mutex_unlock(g_bleAdapterReqRespCbMutex); + OIC_LOG_V(INFO, CALEADAPTER_TAG, "endpoint flags : %d", tmp.endpoint.flags); +#ifdef __WITH_DTLS__ + if (CA_SECURE & tmp.endpoint.flags) + { + OIC_LOG(DEBUG, CALEADAPTER_TAG, "Secure data received"); + switch (receiverType) + { + case ADAPTER_CLIENT: + g_dataType = CA_REQUEST_DATA; + break; + case ADAPTER_SERVER: + g_dataType = CA_RESPONSE_DATA; + break; + default: + OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Unsupported rcvr type:%d",receiverType); + oc_mutex_unlock(g_bleAdapterReqRespCbMutex); + u_arraylist_remove(bleData->senderInfo, senderIndex); + senderInfo->remoteEndpoint = NULL; + senderInfo->defragData = NULL; + OICFree(senderInfo); + oc_mutex_unlock(bleReceiveDataMutex); + return; + } + + if (CA_STATUS_FAILED == CAdecryptSsl(&tmp, + senderInfo->defragData, + senderInfo->recvDataLen)) + { + OIC_LOG(ERROR, CALEADAPTER_TAG, "CAdecryptSsl failed"); + } + else + { + OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAdecryptSsl successed"); + } + OICFree(senderInfo->defragData); + } + else + { +#endif + OIC_LOG(INFO, CALEADAPTER_TAG, "Non-Secure data received"); + g_networkPacketReceivedCallback(&tmp, + senderInfo->defragData, + senderInfo->recvDataLen); +#ifdef __WITH_DTLS__ + } +#endif + + oc_mutex_unlock(g_bleAdapterReqRespCbMutex); u_arraylist_remove(bleData->senderInfo, senderIndex); senderInfo->remoteEndpoint = NULL; senderInfo->defragData = NULL; OICFree(senderInfo); } } - ca_mutex_unlock(g_bleReceiveDataMutex); - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); + oc_mutex_unlock(bleReceiveDataMutex); +} + + +static void CALEServerDataReceiverHandler(void *threadData) +{ + CALEDataReceiverHandler(threadData, ADAPTER_SERVER); +} + +static void CALEClientDataReceiverHandler(void *threadData) +{ + CALEDataReceiverHandler(threadData, ADAPTER_CLIENT); } static void CALEServerSendDataThread(void *threadData) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEServerSendDataThread"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__); CALEData_t * const bleData = (CALEData_t *) threadData; if (!bleData) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!"); + OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid bledata!"); + return; + } + + if (!bleData->remoteEndpoint) + { + OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid endpoint of bledata!"); return; } +#if defined(__TIZEN__) || defined(__ANDROID__) + // get MTU size + g_mtuSize = CALEServerGetMtuSize(bleData->remoteEndpoint->addr); +#endif + OIC_LOG_V(INFO, CALEADAPTER_TAG, "MTU size [%d]", g_mtuSize); + uint32_t midPacketCount = 0; size_t remainingLen = 0; size_t totalLength = 0; @@ -1038,7 +1236,8 @@ static void CALEServerSendDataThread(void *threadData) CAResult_t result = CAGenerateVariableForFragmentation(bleData->dataLen, &midPacketCount, &remainingLen, - &totalLength); + &totalLength, + g_mtuSize); if (CA_STATUS_OK != result) { @@ -1049,12 +1248,12 @@ static void CALEServerSendDataThread(void *threadData) } OIC_LOG_V(DEBUG, CALEADAPTER_TAG, - "Packet info: data size[%d] midPacketCount[%d] remainingLen[%d] totalLength[%d]", + "Packet info: data size[%d] midPacketCount[%u] remainingLen[%zu] totalLength[%zu]", bleData->dataLen, midPacketCount, remainingLen, totalLength); OIC_LOG_V(DEBUG, CALEADAPTER_TAG, - "Server total Data length with header is [%u]", + "Server total Data length with header is [%zu]", totalLength); uint8_t dataSegment[CA_SUPPORTED_BLE_MTU_SIZE] = {0}; @@ -1062,9 +1261,10 @@ static void CALEServerSendDataThread(void *threadData) if (NULL != bleData->remoteEndpoint) //Unicast Data { - secureFlag = bleData->remoteEndpoint->flags == CA_SECURE ? + secureFlag = (bleData->remoteEndpoint->flags & CA_SECURE) ? CA_BLE_PACKET_SECURE : CA_BLE_PACKET_NON_SECURE; + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This Packet is secure? %d", secureFlag); result = CAGenerateHeader(dataHeader, CA_BLE_PACKET_START, g_localBLESourcePort, @@ -1079,6 +1279,7 @@ static void CALEServerSendDataThread(void *threadData) secureFlag, CA_BLE_MULTICAST_PORT); } + OIC_LOG_V(INFO, CALEADAPTER_TAG, "header info: secureFlag[%X]", secureFlag); if (CA_STATUS_OK != result) { @@ -1103,15 +1304,15 @@ static void CALEServerSendDataThread(void *threadData) uint32_t length = 0; uint32_t dataLen = 0; - if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength) + if (g_mtuSize > totalLength) { length = totalLength; dataLen = bleData->dataLen; } else { - length = CA_SUPPORTED_BLE_MTU_SIZE; - dataLen = CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE - CA_BLE_LENGTH_HEADER_SIZE; + length = g_mtuSize; + dataLen = g_mtuSize - CA_BLE_HEADER_SIZE - CA_BLE_LENGTH_HEADER_SIZE; } result = CAMakeFirstDataSegment(dataSegment, @@ -1153,7 +1354,7 @@ static void CALEServerSendDataThread(void *threadData) OIC_LOG_V(DEBUG, CALEADAPTER_TAG, - "Server Sent data length [%u]", + "Server Sent Unicast First Data - data length [%zu]", length); result = CAGenerateHeader(dataHeader, @@ -1173,16 +1374,13 @@ static void CALEServerSendDataThread(void *threadData) for (index = 0; index < iter; index++) { // Send the remaining header. - OIC_LOG_V(DEBUG, - CALEADAPTER_TAG, - "Sending the chunk number [%u]", - index); - result = CAMakeRemainDataSegment(dataSegment, + g_mtuSize - CA_BLE_HEADER_SIZE, bleData->data, - CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE, + bleData->dataLen, index, - dataHeader); + dataHeader, + g_mtuSize); if (CA_STATUS_OK != result) { @@ -1196,7 +1394,7 @@ static void CALEServerSendDataThread(void *threadData) CAUpdateCharacteristicsToGattClient( bleData->remoteEndpoint->addr, dataSegment, - CA_SUPPORTED_BLE_MTU_SIZE); + g_mtuSize); if (CA_STATUS_OK != result) { @@ -1206,20 +1404,20 @@ static void CALEServerSendDataThread(void *threadData) return; } OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", - CA_SUPPORTED_BLE_MTU_SIZE); + g_mtuSize); } - if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE)) + if (remainingLen && (totalLength > g_mtuSize)) { // 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 = CAMakeRemainDataSegment(dataSegment, - bleData->data, remainingLen, + bleData->data, + bleData->dataLen, index, - dataHeader); + dataHeader, + g_mtuSize); if (CA_STATUS_OK != result) { @@ -1246,120 +1444,64 @@ static void CALEServerSendDataThread(void *threadData) result); return; } - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, - "Server Sent data length [%d]", remainingLen + CA_BLE_HEADER_SIZE); + OIC_LOG_V(DEBUG, + CALEADAPTER_TAG, + "Server Sent Unicast Last Data - data length [%zu]", + remainingLen + CA_BLE_HEADER_SIZE); } } 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); - CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result); - return; - } - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length); - - result = CAGenerateHeader(dataHeader, - CA_BLE_PACKET_NOT_START, - g_localBLESourcePort, - secureFlag, - CA_BLE_MULTICAST_PORT); - - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "CAGenerateHeader failed, result [%d]", result); - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - return; - } - - for (index = 0; index < iter; index++) - { - // Send the remaining header. - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index); - - result = CAMakeRemainDataSegment(dataSegment, - bleData->data, - CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE, - index, - dataHeader); - - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "Making data segment failed, result [%d]", result); - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - return; - } - - result = CAUpdateCharacteristicsToAllGattClients( - dataSegment, - 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); - return; - } - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%u]", - CA_SUPPORTED_BLE_MTU_SIZE); - } - - if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE)) - { - // send the last segment of the data - OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk"); - - result = CAMakeRemainDataSegment(dataSegment, - bleData->data, - remainingLen, - index, - dataHeader); - - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "Making data segment failed, result [%d]", result); - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - return; - } - - result = CAUpdateCharacteristicsToAllGattClients( - dataSegment, - remainingLen + CA_BLE_HEADER_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); - return; - } - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, - "Server Sent data length [%d]", remainingLen + CA_BLE_HEADER_SIZE); - } + OIC_LOG(DEBUG, CALEADAPTER_TAG, "BLE Multicast is not supported"); } - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CALEServerSendDataThread"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__); } static void CALEClientSendDataThread(void *threadData) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEClientSendDataThread"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__); CALEData_t *bleData = (CALEData_t *) threadData; if (!bleData) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!"); + OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid bledata!"); return; } + if (!bleData->remoteEndpoint) + { + OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid endpoint of bledata!"); + return; + } + +#if defined(__TIZEN__) || defined(__ANDROID__) + // get MTU size + if (false == CALEClientIsConnected(bleData->remoteEndpoint->addr)) + { + // triggering to gatt connect and MTU negotiation + CAResult_t res = CALEClientSendNegotiationMessage( + bleData->remoteEndpoint->addr); + + if (CA_STATUS_OK != res) + { + OIC_LOG_V(ERROR, + CALEADAPTER_TAG, + "CALEClientSendNegotiationMessage has failed, result [%d]", + res); + + g_errorHandler(bleData->remoteEndpoint, + bleData->data, + bleData->dataLen, + res); + return; + } + } + g_mtuSize = CALEClientGetMtuSize(bleData->remoteEndpoint->addr); +#endif + OIC_LOG_V(INFO, CALEADAPTER_TAG, "MTU size [%d]", g_mtuSize); + uint32_t midPacketCount = 0; size_t remainingLen = 0; size_t totalLength = 0; @@ -1368,7 +1510,8 @@ static void CALEClientSendDataThread(void *threadData) CAResult_t result = CAGenerateVariableForFragmentation(bleData->dataLen, &midPacketCount, &remainingLen, - &totalLength); + &totalLength, + g_mtuSize); if (CA_STATUS_OK != result) { @@ -1379,7 +1522,7 @@ static void CALEClientSendDataThread(void *threadData) } OIC_LOG_V(DEBUG, CALEADAPTER_TAG, - "Packet info: data size[%d] midPacketCount[%d] remainingLen[%d] totalLength[%d]", + "Packet info: data size[%d] midPacketCount[%u] remainingLen[%zu] totalLength[%zu]", bleData->dataLen, midPacketCount, remainingLen, totalLength); uint8_t dataSegment[CA_SUPPORTED_BLE_MTU_SIZE] = {0}; @@ -1387,9 +1530,10 @@ static void CALEClientSendDataThread(void *threadData) if (NULL != bleData->remoteEndpoint) //Unicast Data { - secureFlag = bleData->remoteEndpoint->flags == CA_SECURE ? + secureFlag = (bleData->remoteEndpoint->flags & CA_SECURE) ? CA_BLE_PACKET_SECURE : CA_BLE_PACKET_NON_SECURE; + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This Packet is secure? %d", secureFlag); result = CAGenerateHeader(dataHeader, CA_BLE_PACKET_START, g_localBLESourcePort, @@ -1405,6 +1549,8 @@ static void CALEClientSendDataThread(void *threadData) CA_BLE_MULTICAST_PORT); } + OIC_LOG_V(INFO, CALEADAPTER_TAG, "header info: secureFlag[%X]", secureFlag); + if (CA_STATUS_OK != result) { OIC_LOG_V(ERROR, CALEADAPTER_TAG, @@ -1428,15 +1574,15 @@ static void CALEClientSendDataThread(void *threadData) uint32_t length = 0; uint32_t dataLen = 0; - if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength) + if (g_mtuSize > totalLength) { length = totalLength; dataLen = bleData->dataLen; } else { - length = CA_SUPPORTED_BLE_MTU_SIZE; - dataLen = CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE - CA_BLE_LENGTH_HEADER_SIZE; + length = g_mtuSize; + dataLen = g_mtuSize - CA_BLE_HEADER_SIZE - CA_BLE_LENGTH_HEADER_SIZE; } result = CAMakeFirstDataSegment(dataSegment, @@ -1463,133 +1609,30 @@ static void CALEClientSendDataThread(void *threadData) dataSegment, length, 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); - return; - } - - OIC_LOG_V(DEBUG, - CALEADAPTER_TAG, - "Client Sent Data length is [%u]", - length); - - result = CAGenerateHeader(dataHeader, - CA_BLE_PACKET_NOT_START, - g_localBLESourcePort, - secureFlag, - bleData->remoteEndpoint->port); - - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "CAGenerateHeader failed, result [%d]", result); - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - return; - } - - for (index = 0; index < iter; index++) - { - result = CAMakeRemainDataSegment(dataSegment, - bleData->data, - CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE, - index, - dataHeader); - - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "Making data segment failed, result [%d]", result); - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - return; - } - - // Send the remaining header. - result = CAUpdateCharacteristicsToGattServer( - bleData->remoteEndpoint->addr, - dataSegment, - 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); - return; - } - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", - 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 = CAMakeRemainDataSegment(dataSegment, - bleData->data, - remainingLen, - index, - dataHeader); - - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "Making data segment failed, result [%d]", result); - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - return; - } - - result = CAUpdateCharacteristicsToGattServer( - bleData->remoteEndpoint->addr, - dataSegment, - remainingLen + CA_BLE_HEADER_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); - return; - } - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, - "Client Sent Data length is [%d]", remainingLen + CA_BLE_HEADER_SIZE); - } - } - else - { - //Sending Mulitcast Data - // Send the first segment with the header. - OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending Multicast Data"); - result = CAUpdateCharacteristicsToAllGattServers(dataSegment, length); + 0); + 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); - return ; + OIC_LOG_V(ERROR, + CALEADAPTER_TAG, + "Update characteristics failed, result [%d]", + result); + g_errorHandler(bleData->remoteEndpoint, + bleData->data, + bleData->dataLen, + result); + return; } - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", length); + OIC_LOG_V(DEBUG, + CALEADAPTER_TAG, + "Client Sent Unicast First Data - data length [%zu]", + length); result = CAGenerateHeader(dataHeader, CA_BLE_PACKET_NOT_START, g_localBLESourcePort, secureFlag, - 0); + bleData->remoteEndpoint->port); if (CA_STATUS_OK != result) { @@ -1599,14 +1642,15 @@ static void CALEClientSendDataThread(void *threadData) return; } - // Send the remaining header. for (index = 0; index < iter; index++) { result = CAMakeRemainDataSegment(dataSegment, + g_mtuSize - CA_BLE_HEADER_SIZE, bleData->data, - CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE, + bleData->dataLen, index, - dataHeader); + dataHeader, + g_mtuSize); if (CA_STATUS_OK != result) { @@ -1616,32 +1660,40 @@ static void CALEClientSendDataThread(void *threadData) return; } - result = CAUpdateCharacteristicsToAllGattServers( - dataSegment, - CA_SUPPORTED_BLE_MTU_SIZE); + // Send the remaining header. + result = CAUpdateCharacteristicsToGattServer( + bleData->remoteEndpoint->addr, + dataSegment, + g_mtuSize, + LE_UNICAST, 0); if (CA_STATUS_OK != result) { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", + OIC_LOG_V(ERROR, + CALEADAPTER_TAG, + "Update characteristics failed, result [%d]", result); - CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result); + g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); return; } - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Data length is [%d]", - CA_SUPPORTED_BLE_MTU_SIZE); + OIC_LOG_V(DEBUG, + CALEADAPTER_TAG, + "Client Sent Unicast %d Data - data(mtu) length [%zu]", + index + 1, + g_mtuSize); } - if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE)) + if (remainingLen && (totalLength > g_mtuSize)) { // 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 = CAMakeRemainDataSegment(dataSegment, - bleData->data, remainingLen, + bleData->data, + bleData->dataLen, index, - dataHeader); + dataHeader, + g_mtuSize); if (CA_STATUS_OK != result) { @@ -1651,24 +1703,33 @@ static void CALEClientSendDataThread(void *threadData) return; } - result = - CAUpdateCharacteristicsToAllGattServers( - dataSegment, - remainingLen + CA_BLE_HEADER_SIZE); + result = CAUpdateCharacteristicsToGattServer( + bleData->remoteEndpoint->addr, + dataSegment, + remainingLen + CA_BLE_HEADER_SIZE, + LE_UNICAST, 0); 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); + OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", + result); + g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); return; } - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, - "Client Sent Data length is [%d]", remainingLen + CA_BLE_HEADER_SIZE); + OIC_LOG_V(DEBUG, + CALEADAPTER_TAG, + "Client Sent Unicast Last Data - data length [%zu]", + remainingLen + CA_BLE_HEADER_SIZE); } } + else + { + //Sending Mulitcast Data + OIC_LOG(DEBUG, CALEADAPTER_TAG, "Client Sending Multicast Data"); + OIC_LOG(DEBUG, CALEADAPTER_TAG, "BLE Multicast is not supported"); + } - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CABLEClientSendDataThread"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__); } static CALEData_t *CACreateLEData(const CAEndpoint_t *remoteEndpoint, @@ -1717,7 +1778,7 @@ static void CALEDataDestroyer(void *data, uint32_t size) { if ((size_t)size < sizeof(CALEData_t *)) { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Destroy data too small %p %d", data, size); } CALEData_t *ledata = (CALEData_t *) data; @@ -1730,7 +1791,7 @@ static void CALEDataDestroyer(void *data, uint32_t size) static void CALEDataReceiverHandlerSingleThread(const uint8_t *data, uint32_t dataLen) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__); VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL"); @@ -1840,15 +1901,16 @@ static void CALEDataReceiverHandlerSingleThread(const uint8_t *data, data + CA_BLE_HEADER_SIZE, dataOnlyLen); g_singleThreadReceiveData->recvDataLen += dataOnlyLen; - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength [%d] received Datalen [%d]", + OIC_LOG_V(INFO, CALEADAPTER_TAG, "totalDatalength [%d] received Datalen [%d]", g_singleThreadReceiveData->totalDataLen, g_singleThreadReceiveData->recvDataLen); } + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__); } static CAResult_t CALEServerSendDataSingleThread(const uint8_t *data, uint32_t dataLen) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__);); VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL"); @@ -1860,7 +1922,8 @@ static CAResult_t CALEServerSendDataSingleThread(const uint8_t *data, CAResult_t result = CAGenerateVariableForFragmentation(dataLen, &midPacketCount, &remainingLen, - &totalLength); + &totalLength, + g_mtuSize); if (CA_STATUS_OK != result) { @@ -1908,15 +1971,15 @@ static CAResult_t CALEServerSendDataSingleThread(const uint8_t *data, uint32_t length = 0; uint32_t dataOnlyLen = 0; - if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength) + if (g_mtuSize > totalLength) { length = totalLength; dataOnlyLen = dataLen; } else { - length = CA_SUPPORTED_BLE_MTU_SIZE; - dataOnlyLen = CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE - CA_BLE_LENGTH_HEADER_SIZE; + length = g_mtuSize; + dataOnlyLen = g_mtuSize - CA_BLE_HEADER_SIZE - CA_BLE_LENGTH_HEADER_SIZE; } result = CAMakeFirstDataSegment(dataSegment, @@ -1960,10 +2023,12 @@ static CAResult_t CALEServerSendDataSingleThread(const uint8_t *data, for (uint32_t iter = 0; iter < dataLimit; iter++) { result = CAMakeRemainDataSegment(dataSegment, + g_mtuSize - CA_BLE_HEADER_SIZE, data, - CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE, + dataLen, iter, - dataHeader); + dataHeader, + g_mtuSize); if (CA_STATUS_OK != result) { @@ -1974,7 +2039,7 @@ static CAResult_t CALEServerSendDataSingleThread(const uint8_t *data, result = CAUpdateCharacteristicsToAllGattClients( dataSegment, - CA_SUPPORTED_BLE_MTU_SIZE); + g_mtuSize); if (CA_STATUS_OK != result) { @@ -1985,16 +2050,18 @@ static CAResult_t CALEServerSendDataSingleThread(const uint8_t *data, CALEDoEvents(); } - if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE)) + if (remainingLen && (totalLength > g_mtuSize)) { // send the last segment of the data OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk"); result = CAMakeRemainDataSegment(dataSegment, - data, remainingLen, + data, + dataLen, dataLimit, - dataHeader); + dataHeader, + g_mtuSize); if (CA_STATUS_OK != result) { @@ -2021,24 +2088,24 @@ static CAResult_t CALEServerSendDataSingleThread(const uint8_t *data, static CAResult_t CAInitLEAdapterMutex() { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEAdapterMutex"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__); if (NULL == g_bleIsServerMutex) { - g_bleIsServerMutex = ca_mutex_new(); + g_bleIsServerMutex = oc_mutex_new(); if (NULL == g_bleIsServerMutex) { - OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed"); + OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed"); return CA_STATUS_FAILED; } } if (NULL == g_bleNetworkCbMutex) { - g_bleNetworkCbMutex = ca_mutex_new(); + g_bleNetworkCbMutex = oc_mutex_new(); if (NULL == g_bleNetworkCbMutex) { - OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed"); + OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed"); CATerminateLEAdapterMutex(); return CA_STATUS_FAILED; } @@ -2046,10 +2113,10 @@ static CAResult_t CAInitLEAdapterMutex() if (NULL == g_bleLocalAddressMutex) { - g_bleLocalAddressMutex = ca_mutex_new(); + g_bleLocalAddressMutex = oc_mutex_new(); if (NULL == g_bleLocalAddressMutex) { - OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed"); + OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed"); CATerminateLEAdapterMutex(); return CA_STATUS_FAILED; } @@ -2057,10 +2124,10 @@ static CAResult_t CAInitLEAdapterMutex() if (NULL == g_bleAdapterThreadPoolMutex) { - g_bleAdapterThreadPoolMutex = ca_mutex_new(); + g_bleAdapterThreadPoolMutex = oc_mutex_new(); if (NULL == g_bleAdapterThreadPoolMutex) { - OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed"); + OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed"); CATerminateLEAdapterMutex(); return CA_STATUS_FAILED; } @@ -2068,10 +2135,10 @@ static CAResult_t CAInitLEAdapterMutex() if (NULL == g_bleClientSendDataMutex) { - g_bleClientSendDataMutex = ca_mutex_new(); + g_bleClientSendDataMutex = oc_mutex_new(); if (NULL == g_bleClientSendDataMutex) { - OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed"); + OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed"); CATerminateLEAdapterMutex(); return CA_STATUS_FAILED; } @@ -2079,10 +2146,10 @@ static CAResult_t CAInitLEAdapterMutex() if (NULL == g_bleServerSendDataMutex) { - g_bleServerSendDataMutex = ca_mutex_new(); + g_bleServerSendDataMutex = oc_mutex_new(); if (NULL == g_bleServerSendDataMutex) { - OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed"); + OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed"); CATerminateLEAdapterMutex(); return CA_STATUS_FAILED; } @@ -2090,58 +2157,68 @@ static CAResult_t CAInitLEAdapterMutex() if (NULL == g_bleAdapterReqRespCbMutex) { - g_bleAdapterReqRespCbMutex = ca_mutex_new(); + g_bleAdapterReqRespCbMutex = oc_mutex_new(); if (NULL == g_bleAdapterReqRespCbMutex) { - OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed"); + OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed"); CATerminateLEAdapterMutex(); return CA_STATUS_FAILED; } } - if (NULL == g_bleReceiveDataMutex) + if (NULL == g_bleServerReceiveDataMutex) + { + g_bleServerReceiveDataMutex = oc_mutex_new(); + if (NULL == g_bleServerReceiveDataMutex) + { + OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed"); + return CA_STATUS_FAILED; + } + } + + if (NULL == g_bleClientReceiveDataMutex) { - g_bleReceiveDataMutex = ca_mutex_new(); - if (NULL == g_bleReceiveDataMutex) + g_bleClientReceiveDataMutex = oc_mutex_new(); + if (NULL == g_bleClientReceiveDataMutex) { - OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_mutex_new failed"); + OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed"); return CA_STATUS_FAILED; } } - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); return CA_STATUS_OK; } static void CATerminateLEAdapterMutex() { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CATerminateLEAdapterMutex"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__); - ca_mutex_free(g_bleIsServerMutex); + oc_mutex_free(g_bleIsServerMutex); g_bleIsServerMutex = NULL; - ca_mutex_free(g_bleNetworkCbMutex); + oc_mutex_free(g_bleNetworkCbMutex); g_bleNetworkCbMutex = NULL; - ca_mutex_free(g_bleLocalAddressMutex); + oc_mutex_free(g_bleLocalAddressMutex); g_bleLocalAddressMutex = NULL; - ca_mutex_free(g_bleAdapterThreadPoolMutex); + oc_mutex_free(g_bleAdapterThreadPoolMutex); g_bleAdapterThreadPoolMutex = NULL; - ca_mutex_free(g_bleClientSendDataMutex); + oc_mutex_free(g_bleClientSendDataMutex); g_bleClientSendDataMutex = NULL; - ca_mutex_free(g_bleServerSendDataMutex); + oc_mutex_free(g_bleServerSendDataMutex); g_bleServerSendDataMutex = NULL; - ca_mutex_free(g_bleAdapterReqRespCbMutex); + oc_mutex_free(g_bleAdapterReqRespCbMutex); g_bleAdapterReqRespCbMutex = NULL; - ca_mutex_free(g_bleReceiveDataMutex); - g_bleReceiveDataMutex = NULL; + oc_mutex_free(g_bleServerReceiveDataMutex); + g_bleServerReceiveDataMutex = NULL; - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); + oc_mutex_free(g_bleClientReceiveDataMutex); + g_bleClientReceiveDataMutex = NULL; } /** @@ -2345,7 +2422,14 @@ static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint, static CAResult_t CALEAdapterGattServerStart() { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattServer"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__); + + if (caglobals.bleFlags & CA_LE_SERVER_DISABLE) + { + OIC_LOG_V(INFO, CALEADAPTER_TAG, "server flag of configure is disable [%d]", + caglobals.bleFlags); + return CA_STATUS_OK; + } CAResult_t result = CAStartLEGattServer(); @@ -2356,9 +2440,9 @@ static CAResult_t CALEAdapterGattServerStart() */ if (CA_STATUS_OK == result) { - ca_mutex_lock(g_bleServerSendDataMutex); + oc_mutex_lock(g_bleServerSendDataMutex); result = CAQueueingThreadStart(g_bleServerSendQueueHandle); - ca_mutex_unlock(g_bleServerSendDataMutex); + oc_mutex_unlock(g_bleServerSendDataMutex); if (CA_STATUS_OK != result) { @@ -2375,18 +2459,32 @@ static CAResult_t CALEAdapterGattServerStart() static CAResult_t CALEAdapterGattServerStop() { + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__); + + if (caglobals.bleFlags & CA_LE_SERVER_DISABLE) + { + OIC_LOG_V(INFO, CALEADAPTER_TAG, "server flag of configure is disable [%d]", + caglobals.bleFlags); + return CA_STATUS_OK; + } + #ifndef SINGLE_THREAD - OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEAdapterGattServerStop"); - CAResult_t result = CAStopLEGattServer(); - ca_mutex_lock(g_bleServerSendDataMutex); - if (CA_STATUS_OK == result) + oc_mutex_lock(g_bleServerSendDataMutex); + CAResult_t res = CAQueueingThreadStop(g_bleServerSendQueueHandle); + if (CA_STATUS_OK != res) { - result = CAQueueingThreadStop(g_bleServerSendQueueHandle); + OIC_LOG(ERROR, CALEADAPTER_TAG, "CAQueueingThreadStop has failed"); } - ca_mutex_unlock(g_bleServerSendDataMutex); + oc_mutex_unlock(g_bleServerSendDataMutex); - return result; + res = CAStopLEGattServer(); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, CALEADAPTER_TAG, "CAStopLEGattServer has failed"); + } + + return res; #else return CAStopLEGattServer(); #endif @@ -2394,7 +2492,7 @@ static CAResult_t CALEAdapterGattServerStop() static CAResult_t CALEAdapterGattClientStart() { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "Before CAStartLEGattClient"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__); CAResult_t result = CAStartLEGattClient(); @@ -2405,9 +2503,9 @@ static CAResult_t CALEAdapterGattClientStart() */ if (CA_STATUS_OK == result) { - ca_mutex_lock(g_bleClientSendDataMutex); + oc_mutex_lock(g_bleClientSendDataMutex); result = CAQueueingThreadStart(g_bleClientSendQueueHandle); - ca_mutex_unlock(g_bleClientSendDataMutex); + oc_mutex_unlock(g_bleClientSendDataMutex); if (CA_STATUS_OK != result) { @@ -2424,12 +2522,12 @@ static CAResult_t CALEAdapterGattClientStart() static CAResult_t CALEAdapterGattClientStop() { #ifndef SINGLE_THREAD - OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEAdapterGattClientStop"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__); CAStopLEGattClient(); - ca_mutex_lock(g_bleClientSendDataMutex); + oc_mutex_lock(g_bleClientSendDataMutex); CAResult_t result = CAQueueingThreadStop(g_bleClientSendQueueHandle); - ca_mutex_unlock(g_bleClientSendDataMutex); + oc_mutex_unlock(g_bleClientSendDataMutex); return result; #else @@ -2439,6 +2537,92 @@ static CAResult_t CALEAdapterGattClientStop() #endif } +#ifdef __WITH_DTLS__ +static ssize_t CALESecureSendDataCB(CAEndpoint_t *endpoint, const void *data, size_t dataLen) +{ + VERIFY_NON_NULL(endpoint, CALEADAPTER_TAG, "endpoint is NULL"); + VERIFY_NON_NULL(data, CALEADAPTER_TAG, "data is NULL"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "encrypted datalen = %d", dataLen); + + CAResult_t result; + CADataType_t dataType = g_dataType; + ssize_t ret = 0; + + if (ADAPTER_SERVER == g_adapterType || + (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_DATA == dataType)) + { + result = CALEAdapterServerSendData(endpoint, data, dataLen); + if (CA_STATUS_OK != result) + { + OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed" ); + + if (g_errorHandler) + { + g_errorHandler(endpoint, data, dataLen, result); + } + return ret; + } + ret = (ssize_t)dataLen; + } + else if (ADAPTER_CLIENT == g_adapterType || + (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_REQUEST_DATA == dataType) || + (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_FOR_RES == dataType)) + { + result = CALEAdapterClientSendData(endpoint, data, dataLen); + if (CA_STATUS_OK != result) + { + OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for client failed" ); + + if (g_errorHandler) + { + g_errorHandler(endpoint, data, dataLen, result); + } + return ret; + } + ret = (ssize_t)dataLen; + } + else + { + OIC_LOG_V(ERROR, CALEADAPTER_TAG, + "Can't Send Message adapterType = %d, dataType = %d", g_adapterType, dataType); + return ret; + } + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__); + return ret; +} + +void CALESecureReceiveDataCB(const CASecureEndpoint_t *sep, const void *data, + size_t dataLen) +{ + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__); + + VERIFY_NON_NULL_VOID(sep, CALEADAPTER_TAG, "sep is NULL"); + VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "data is NULL"); + + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, + "Secure Data Receive - decrypted datalen = %d", dataLen); + + if (dataLen <= 0) + { + OIC_LOG(ERROR, CALEADAPTER_TAG, "incorrect dataLen, derecypt fail !"); + return; + } + + OIC_LOG_BUFFER(DEBUG, CALEADAPTER_TAG, data, dataLen); + + if (g_networkPacketReceivedCallback) + { + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, + "[CALESecureReceiveDataCB] Secure flags = %d, %x", + sep->endpoint.flags, sep->endpoint.flags); + OIC_LOG(DEBUG, CALEADAPTER_TAG, + "[CALESecureReceiveDataCB] Received data up !"); + g_networkPacketReceivedCallback(sep, data, dataLen); + } +} +#endif + CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback, CANetworkPacketReceivedCallback reqRespCallback, CAAdapterChangeCallback netCallback, @@ -2446,7 +2630,7 @@ CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback, CAErrorHandleCallback errorCallback, ca_thread_pool_t handle) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__); //Input validation VERIFY_NON_NULL(registerCallback, CALEADAPTER_TAG, "RegisterConnectivity callback is null"); @@ -2470,8 +2654,6 @@ CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback, } CAInitializeLEAdapter(); - CASetLEClientThreadPoolHandle(handle); - result = CAInitializeLEGattClient(); if (CA_STATUS_OK != result) { @@ -2479,6 +2661,8 @@ CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback, return CA_STATUS_FAILED; } + CASetLEClientThreadPoolHandle(handle); + CASetLEReqRespClientCallback(CALEAdapterClientReceivedData); CASetLEServerThreadPoolHandle(handle); result = CAInitializeLEGattServer(); @@ -2498,6 +2682,18 @@ CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback, g_errorHandler = errorCallback; +#ifdef __WITH_DTLS__ + if (CA_STATUS_OK != CAinitSslAdapter()) + { + OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to init SSL adapter"); + } + else + { + CAsetSslAdapterCallbacks(CALESecureReceiveDataCB, CALESecureSendDataCB, + CA_ADAPTER_GATT_BTLE); + } +#endif + static const CAConnectivityHandler_t connHandler = { .startAdapter = CAStartLE, @@ -2514,27 +2710,25 @@ CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback, }; registerCallback(connHandler); - - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); - return CA_STATUS_OK; } static CAResult_t CAStartLE() { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAStartLE"); - return CAStartLEAdapter(); } static CAResult_t CAStopLE() { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN"); +#ifdef __WITH_DTLS__ + CAdeinitSslAdapter(); +#endif + #ifndef SINGLE_THREAD CAStopLEQueues(); #endif - ca_mutex_lock(g_bleIsServerMutex); + oc_mutex_lock(g_bleIsServerMutex); switch (g_adapterType) { case ADAPTER_SERVER: @@ -2550,16 +2744,13 @@ static CAResult_t CAStopLE() default: break; } - ca_mutex_unlock(g_bleIsServerMutex); - - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); - + oc_mutex_unlock(g_bleIsServerMutex); return CAStopLEAdapter(); } static void CATerminateLE() { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__); CASetLEReqRespServerCallback(NULL); CASetLEReqRespClientCallback(NULL); @@ -2567,7 +2758,7 @@ static void CATerminateLE() CASetLEReqRespAdapterCallback(NULL); CATerminateLENetworkMonitor(); - ca_mutex_lock(g_bleIsServerMutex); + oc_mutex_lock(g_bleIsServerMutex); switch (g_adapterType) { case ADAPTER_SERVER: @@ -2584,19 +2775,30 @@ static void CATerminateLE() break; } g_adapterType = ADAPTER_EMPTY; - ca_mutex_unlock(g_bleIsServerMutex); + oc_mutex_unlock(g_bleIsServerMutex); #ifndef SINGLE_THREAD CATerminateLEQueues(); #endif - CATerminateLEAdapterMutex(); - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); +#ifdef __WITH_DTLS__ + CAsetSslAdapterCallbacks(NULL, NULL, CA_ADAPTER_GATT_BTLE); +#endif + + CATerminateLEAdapterMutex(); } static CAResult_t CAStartLEListeningServer() { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEListeningServer"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__); + + if (caglobals.bleFlags & CA_LE_SERVER_DISABLE) + { + OIC_LOG_V(INFO, CALEADAPTER_TAG, "server flag of configure is disable [%d]", + caglobals.bleFlags); + return CA_STATUS_OK; + } + #ifndef ROUTING_GATEWAY CAResult_t result = CA_STATUS_OK; #ifndef SINGLE_THREAD @@ -2608,7 +2810,7 @@ static CAResult_t CAStartLEListeningServer() } #endif - ca_mutex_lock(g_bleIsServerMutex); + oc_mutex_lock(g_bleIsServerMutex); switch (g_adapterType) { case ADAPTER_CLIENT: @@ -2619,7 +2821,7 @@ static CAResult_t CAStartLEListeningServer() default: g_adapterType = ADAPTER_SERVER; } - ca_mutex_unlock(g_bleIsServerMutex); + oc_mutex_unlock(g_bleIsServerMutex); result = CAGetLEAdapterState(); if (CA_STATUS_OK != result) @@ -2637,7 +2839,7 @@ static CAResult_t CAStartLEListeningServer() result = CALEAdapterGattServerStart(); } - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__); return result; #else // Routing Gateway only supports BLE client mode. @@ -2654,7 +2856,7 @@ static CAResult_t CAStopLEListeningServer() static CAResult_t CAStartLEDiscoveryServer() { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStartLEDiscoveryServer"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__); CAResult_t result = CA_STATUS_OK; #ifndef SINGLE_THREAD result = CAInitLEClientQueues(); @@ -2665,7 +2867,7 @@ static CAResult_t CAStartLEDiscoveryServer() } #endif - ca_mutex_lock(g_bleIsServerMutex); + oc_mutex_lock(g_bleIsServerMutex); switch (g_adapterType) { case ADAPTER_SERVER: @@ -2676,7 +2878,7 @@ static CAResult_t CAStartLEDiscoveryServer() default: g_adapterType = ADAPTER_CLIENT; } - ca_mutex_unlock(g_bleIsServerMutex); + oc_mutex_unlock(g_bleIsServerMutex); result = CAGetLEAdapterState(); if (CA_STATUS_OK != result) @@ -2694,17 +2896,15 @@ static CAResult_t CAStartLEDiscoveryServer() result = CALEAdapterGattClientStart(); } - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__); return result; } static CAResult_t CAReadLEData() { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN"); #ifdef SINGLE_THREAD CACheckLEData(); #endif - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); return CA_STATUS_OK; } @@ -2713,7 +2913,8 @@ static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint, uint32_t dataLen, CADataType_t dataType) { - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN - CASendLEUnicastData : type(%d)", dataType); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "type(%d)", dataType); //Input validation VERIFY_NON_NULL_RET(endpoint, CALEADAPTER_TAG, "Remote endpoint is null", -1); @@ -2727,14 +2928,39 @@ static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint, OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty"); } - ca_mutex_lock(g_bleIsServerMutex); + oc_mutex_lock(g_bleIsServerMutex); if (ADAPTER_SERVER == g_adapterType || (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_DATA == dataType)) { +#ifdef __WITH_DTLS__ + if (endpoint && endpoint->flags & CA_SECURE) + { + OIC_LOG(DEBUG, CALEADAPTER_TAG, + "Response Data or server - secured data send(caadapternetdtlsencrypt) call"); + OIC_LOG_BUFFER(DEBUG, CALEADAPTER_TAG, data, dataLen); + g_dataType = dataType; + oc_mutex_unlock(g_bleIsServerMutex); + + result = CAencryptSsl(endpoint, data, dataLen); + if (CA_STATUS_OK != result) + { + OIC_LOG(ERROR, CALEADAPTER_TAG, "caadapternetdtlsencrypt failed!"); + return -1; + } + return dataLen; + } + else + { + OIC_LOG(DEBUG, CALEADAPTER_TAG, + "server or both - none secured data send(CALEAdapterServerSendData) call"); + result = CALEAdapterServerSendData(endpoint, data, dataLen); + } +#else result = CALEAdapterServerSendData(endpoint, data, dataLen); +#endif if (CA_STATUS_OK != result) { - ca_mutex_unlock(g_bleIsServerMutex); + oc_mutex_unlock(g_bleIsServerMutex); OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed"); if (g_errorHandler) { @@ -2744,27 +2970,51 @@ static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint, return -1; } } - - if (ADAPTER_CLIENT == g_adapterType || + else if (ADAPTER_CLIENT == g_adapterType || (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_REQUEST_DATA == dataType) || (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_FOR_RES == dataType)) { +#ifdef __WITH_DTLS__ + if (endpoint && endpoint->flags & CA_SECURE) + { + OIC_LOG(DEBUG, CALEADAPTER_TAG, + "Request Data or client - secured data send(caadapternetdtlsencrypt) call"); + OIC_LOG_BUFFER(DEBUG, CALEADAPTER_TAG, data, dataLen); + g_dataType = dataType; + oc_mutex_unlock(g_bleIsServerMutex); + + result = CAencryptSsl(endpoint, data, dataLen); + if (CA_STATUS_OK != result) + { + OIC_LOG(ERROR, CALEADAPTER_TAG, "caadapternetdtlsencrypt failed!"); + return -1; + } + return dataLen; + } + else + { + OIC_LOG(DEBUG, CALEADAPTER_TAG, + "client or both - none secured data send(CALEAdapterClientSendData) call"); + result = CALEAdapterClientSendData(endpoint, data, dataLen); + } +#else result = CALEAdapterClientSendData(endpoint, data, dataLen); +#endif if (CA_STATUS_OK != result) { - ca_mutex_unlock(g_bleIsServerMutex); + oc_mutex_unlock(g_bleIsServerMutex); OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for client failed" ); - if (g_errorHandler) - { - g_errorHandler(endpoint, data, dataLen, result); - } + if (g_errorHandler) + { + g_errorHandler(endpoint, data, dataLen, result); + } return -1; } } - ca_mutex_unlock(g_bleIsServerMutex); + oc_mutex_unlock(g_bleIsServerMutex); - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__); return dataLen; } @@ -2773,7 +3023,7 @@ static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint, uint32_t dataLen, CADataType_t dataType) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CASendLEMulticastData"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__); //Input validation VERIFY_NON_NULL_RET(data, CALEADAPTER_TAG, "Data is null", -1); @@ -2792,14 +3042,14 @@ static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint, OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty"); } - ca_mutex_lock(g_bleIsServerMutex); + oc_mutex_lock(g_bleIsServerMutex); if (ADAPTER_SERVER == g_adapterType || (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_DATA == dataType)) { result = CALEAdapterServerSendData(NULL, data, dataLen); if (CA_STATUS_OK != result) { - ca_mutex_unlock(g_bleIsServerMutex); + oc_mutex_unlock(g_bleIsServerMutex); OIC_LOG(ERROR, CALEADAPTER_TAG, "Send multicast data for server failed" ); @@ -2818,7 +3068,7 @@ static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint, result = CALEAdapterClientSendData(NULL, data, dataLen); if (CA_STATUS_OK != result) { - ca_mutex_unlock(g_bleIsServerMutex); + oc_mutex_unlock(g_bleIsServerMutex); OIC_LOG(ERROR, CALEADAPTER_TAG, "Send Multicast data for client failed" ); @@ -2829,15 +3079,15 @@ static int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint, return -1; } } - ca_mutex_unlock(g_bleIsServerMutex); + oc_mutex_unlock(g_bleIsServerMutex); - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CASendLEMulticastData"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__); return dataLen; } static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__); VERIFY_NON_NULL(info, CALEADAPTER_TAG, "CALocalConnectivity info is null"); @@ -2877,27 +3127,27 @@ static CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *siz } OICStrcpy((*info)->addr, sizeof((*info)->addr), local_address); - ca_mutex_lock(g_bleLocalAddressMutex); + oc_mutex_lock(g_bleLocalAddressMutex); OICStrcpy(g_localBLEAddress, sizeof(g_localBLEAddress), local_address); - ca_mutex_unlock(g_bleLocalAddressMutex); + oc_mutex_unlock(g_bleLocalAddressMutex); (*info)->adapter = CA_ADAPTER_GATT_BTLE; *size = 1; OICFree(local_address); - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__); return CA_STATUS_OK; } static CAResult_t CALERegisterNetworkNotifications(CAAdapterChangeCallback netCallback, CAConnectionChangeCallback connCallback) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__); - ca_mutex_lock(g_bleNetworkCbMutex); + oc_mutex_lock(g_bleNetworkCbMutex); g_networkCallback = netCallback; g_connectionCallback = connCallback; - ca_mutex_unlock(g_bleNetworkCbMutex); + oc_mutex_unlock(g_bleNetworkCbMutex); CAResult_t res = CA_STATUS_OK; if (netCallback) { @@ -2924,21 +3174,32 @@ static CAResult_t CALERegisterNetworkNotifications(CAAdapterChangeCallback netCa OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLENWConnectionStateChangedCb failed!"); } } + else + { + res = CAUnSetLENWConnectionStateChangedCb(); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, CALEADAPTER_TAG, "CAUnSetLENWConnectionStateChangedCb failed!"); + } + } - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__); return res; } static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address, bool isConnected) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEConnectionStateChangedCb"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__); VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address"); (void)adapter; + CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE }; + OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), address); + #ifdef __TIZEN__ - ca_mutex_lock(g_bleIsServerMutex); + oc_mutex_lock(g_bleIsServerMutex); switch (g_adapterType) { case ADAPTER_SERVER: @@ -2954,7 +3215,7 @@ static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const cha default: break; } - ca_mutex_unlock(g_bleIsServerMutex); + oc_mutex_unlock(g_bleIsServerMutex); #endif if(!isConnected) @@ -2985,28 +3246,27 @@ static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const cha address); } #endif - } - CAEndpoint_t localEndpoint = { .adapter = CA_ADAPTER_GATT_BTLE }; - OICStrcpy(localEndpoint.addr, sizeof(localEndpoint.addr), address); +#ifdef __WITH_DTLS__ + CAcloseSslConnection(&localEndpoint); +#endif + } - ca_mutex_lock(g_bleNetworkCbMutex); if (g_connectionCallback) { g_connectionCallback(&localEndpoint, isConnected); } - ca_mutex_unlock(g_bleNetworkCbMutex); - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__); } static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDeviceStateChangedCb"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__); if (CA_ADAPTER_ENABLED == adapter_state) { - ca_mutex_lock(g_bleIsServerMutex); + oc_mutex_lock(g_bleIsServerMutex); switch (g_adapterType) { case ADAPTER_SERVER: @@ -3022,11 +3282,11 @@ static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state) default: break; } - ca_mutex_unlock(g_bleIsServerMutex); + oc_mutex_unlock(g_bleIsServerMutex); } else { - ca_mutex_lock(g_bleIsServerMutex); + oc_mutex_lock(g_bleIsServerMutex); switch (g_adapterType) { case ADAPTER_SERVER: @@ -3042,10 +3302,9 @@ static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state) default: break; } - ca_mutex_unlock(g_bleIsServerMutex); + oc_mutex_unlock(g_bleIsServerMutex); } - ca_mutex_lock(g_bleNetworkCbMutex); if (NULL != g_networkCallback) { g_networkCallback(CA_ADAPTER_GATT_BTLE, adapter_state); @@ -3054,17 +3313,14 @@ static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state) { OIC_LOG(ERROR, CALEADAPTER_TAG, "g_networkCallback is NULL"); } - ca_mutex_unlock(g_bleNetworkCbMutex); - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__); } static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint, const uint8_t *data, uint32_t dataLen) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN"); - VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL"); #ifndef SINGLE_THREAD VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, @@ -3074,10 +3330,6 @@ static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint, "g_bleClientSendDataMutex is NULL", CA_STATUS_FAILED); - VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG, - "g_bleClientSendQueueHandle", - CA_STATUS_FAILED); - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen); CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen, NULL); @@ -3087,11 +3339,10 @@ static CAResult_t CALEAdapterClientSendData(const CAEndpoint_t *remoteEndpoint, return CA_MEMORY_ALLOC_FAILED; } // Add message to send queue - ca_mutex_lock(g_bleClientSendDataMutex); + oc_mutex_lock(g_bleClientSendDataMutex); CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t)); - ca_mutex_unlock(g_bleClientSendDataMutex); + oc_mutex_unlock(g_bleClientSendDataMutex); #endif - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); return CA_STATUS_OK; } @@ -3099,7 +3350,7 @@ static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint, const uint8_t *data, uint32_t dataLen) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__); VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL"); @@ -3139,13 +3390,13 @@ static CAResult_t CALEAdapterServerSendData(const CAEndpoint_t *remoteEndpoint, } // Add message to send queue - ca_mutex_lock(g_bleServerSendDataMutex); + oc_mutex_lock(g_bleServerSendDataMutex); CAQueueingThreadAddData(g_bleServerSendQueueHandle, bleData, sizeof(CALEData_t)); - ca_mutex_unlock(g_bleServerSendDataMutex); + oc_mutex_unlock(g_bleServerSendDataMutex); #endif - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__); return CA_STATUS_OK; } @@ -3154,7 +3405,7 @@ static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress, uint32_t dataLength, uint32_t *sentLength) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__); //Input validation VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Data is null"); @@ -3176,14 +3427,15 @@ static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress, g_singleThreadReceiveData->recvDataLen); } g_singleThreadReceiveData->remoteEndpoint = NULL; + OICFree(g_singleThreadReceiveData->defragData); g_singleThreadReceiveData->defragData = NULL; OICFree(g_singleThreadReceiveData); g_singleThreadReceiveData = NULL; } #else - VERIFY_NON_NULL_RET(g_bleReceiverQueue, + VERIFY_NON_NULL_RET(g_bleServerReceiverQueue, CALEADAPTER_TAG, - "g_bleReceiverQueue", + "g_bleServerReceiverQueue", CA_STATUS_FAILED); //Add message to data queue @@ -3202,7 +3454,7 @@ static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress, // Create bleData to add to queue OIC_LOG_V(DEBUG, CALEADAPTER_TAG, - "Data received from LE layer [%d]", + "Data received from LE Server layer [%d]", dataLength); CALEData_t * const bleData = @@ -3217,11 +3469,11 @@ static CAResult_t CALEAdapterServerReceivedData(const char *remoteAddress, CAFreeEndpoint(remoteEndpoint); // Add message to receiver queue - CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t)); + CAQueueingThreadAddData(g_bleServerReceiverQueue, bleData, sizeof(CALEData_t)); *sentLength = dataLength; #endif - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__); return CA_STATUS_OK; } @@ -3230,14 +3482,12 @@ static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress, uint32_t dataLength, uint32_t *sentLength) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN"); - //Input validation 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_bleReceiverQueue, CALEADAPTER_TAG, - "g_bleReceiverQueue", + VERIFY_NON_NULL_RET(g_bleClientReceiverQueue, CALEADAPTER_TAG, + "g_bleClientReceiverQueue", CA_STATUS_FAILED); //Add message to data queue @@ -3250,11 +3500,15 @@ static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress, return CA_STATUS_FAILED; } - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data received from LE layer [%u]", dataLength); - // Create bleData to add to queue - CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, - dataLength, g_bleClientSenderInfo); + OIC_LOG_V(DEBUG, + CALEADAPTER_TAG, + "Data received from LE Client layer [%zu]", + dataLength); + + CALEData_t * const bleData = + CACreateLEData(remoteEndpoint, data, dataLength, g_bleClientSenderInfo); + if (!bleData) { OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to create bledata!"); @@ -3264,36 +3518,27 @@ static CAResult_t CALEAdapterClientReceivedData(const char *remoteAddress, CAFreeEndpoint(remoteEndpoint); // Add message to receiver queue - CAQueueingThreadAddData(g_bleReceiverQueue, bleData, sizeof(CALEData_t)); + CAQueueingThreadAddData(g_bleClientReceiverQueue, bleData, sizeof(CALEData_t)); *sentLength = dataLength; #endif - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); return CA_STATUS_OK; } static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN"); - - ca_mutex_lock(g_bleAdapterThreadPoolMutex); + oc_mutex_lock(g_bleAdapterThreadPoolMutex); g_bleAdapterThreadPool = handle; - ca_mutex_unlock(g_bleAdapterThreadPoolMutex); - - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); + oc_mutex_unlock(g_bleAdapterThreadPoolMutex); } static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN"); - - ca_mutex_lock(g_bleAdapterReqRespCbMutex); + oc_mutex_lock(g_bleAdapterReqRespCbMutex); g_networkPacketReceivedCallback = callback; - ca_mutex_unlock(g_bleAdapterReqRespCbMutex); - - OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT"); + oc_mutex_unlock(g_bleAdapterReqRespCbMutex); } static void CALEErrorHandler(const char *remoteAddress, @@ -3301,7 +3546,7 @@ static void CALEErrorHandler(const char *remoteAddress, uint32_t dataLen, CAResult_t result) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler IN"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__); VERIFY_NON_NULL_VOID(data, CALEADAPTER_TAG, "Data is null"); @@ -3315,19 +3560,19 @@ static void CALEErrorHandler(const char *remoteAddress, CAFreeEndpoint(rep); - OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__); } #ifndef SINGLE_THREAD -static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle, ca_mutex mutex, +static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle, oc_mutex mutex, const char* address) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALERemoveSendQueueData"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__); VERIFY_NON_NULL_VOID(queueHandle, CALEADAPTER_TAG, "queueHandle"); VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address"); - ca_mutex_lock(mutex); + oc_mutex_lock(mutex); while (u_queue_get_size(queueHandle->dataQueue) > 0) { OIC_LOG(DEBUG, CALEADAPTER_TAG, "get data from queue"); @@ -3337,7 +3582,7 @@ static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle, ca_mutex mu CALEData_t *bleData = (CALEData_t *) message->msg; if (bleData && bleData->remoteEndpoint) { - if (!strcmp(bleData->remoteEndpoint->addr, address)) + if (!strcasecmp(bleData->remoteEndpoint->addr, address)) { OIC_LOG(DEBUG, CALEADAPTER_TAG, "found the message of disconnected device"); if (NULL != queueHandle->destroy) @@ -3354,12 +3599,12 @@ static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle, ca_mutex mu } } } - ca_mutex_unlock(mutex); + oc_mutex_unlock(mutex); } static void CALERemoveReceiveQueueData(u_arraylist_t *dataInfoList, const char* address) { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALERemoveReceiveQueueData"); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__); VERIFY_NON_NULL_VOID(dataInfoList, CALEADAPTER_TAG, "dataInfoList"); VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address"); @@ -3373,10 +3618,17 @@ static void CALERemoveReceiveQueueData(u_arraylist_t *dataInfoList, const char* uint32_t arrayLength = u_arraylist_length(portList); for (uint32_t i = 0; i < arrayLength; i++) { - uint16_t port = u_arraylist_get(portList, i); - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "port : %X", port); + uint16_t *port = (uint16_t *)u_arraylist_get(portList, i); + if (!port) + { + OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to get port from sender info !"); + u_arraylist_destroy(portList); + return; + } - if (CA_STATUS_OK == CALEGetSenderInfo(address, port, + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "port : %X", *port); + + if (CA_STATUS_OK == CALEGetSenderInfo(address, *port, dataInfoList, &senderInfo, &senderIndex)) { @@ -3418,7 +3670,14 @@ static CAResult_t CALEGetPortsFromSenderInfo(const char *leAddress, if (!strncmp(info->remoteEndpoint->addr, leAddress, addrLength)) { - u_arraylist_add(portList, (void *)info->remoteEndpoint->port); + uint16_t *port = (uint16_t *)OICMalloc(sizeof(uint16_t)); + if (!port) + { + OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!"); + return CA_MEMORY_ALLOC_FAILED; + } + *port = info->remoteEndpoint->port; + u_arraylist_add(portList, (void *)port); } } @@ -3432,3 +3691,51 @@ static CAResult_t CALEGetPortsFromSenderInfo(const char *leAddress, } } #endif + +void CALEStartGattServer() +{ + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__); + + oc_mutex_lock(g_bleIsServerMutex); + switch (g_adapterType) + { + case ADAPTER_SERVER: + CALEAdapterGattServerStart(); + break; + case ADAPTER_CLIENT: + CALEAdapterGattClientStart(); + break; + case ADAPTER_BOTH_CLIENT_SERVER: + CALEAdapterGattServerStart(); + CALEAdapterGattClientStart(); + break; + default: + break; + } + oc_mutex_unlock(g_bleIsServerMutex); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__); +} + +void CALEStopGattServer() +{ + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__); + + oc_mutex_lock(g_bleIsServerMutex); + switch (g_adapterType) + { + case ADAPTER_SERVER: + CALEAdapterGattServerStop(); + break; + case ADAPTER_CLIENT: + CALEAdapterGattClientStop(); + break; + case ADAPTER_BOTH_CLIENT_SERVER: + CALEAdapterGattServerStop(); + CALEAdapterGattClientStop(); + break; + default: + break; + } + oc_mutex_unlock(g_bleIsServerMutex); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__); +}