uint32_t totalDataLen;
uint8_t *defragData;
CAEndpoint_t *remoteEndpoint;
- } CABLESenderInfo_t;
+ uint8_t refCount;
+} CABLESenderInfo_t;
typedef enum
{
* 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 oc_mutex g_bleIsServerMutex = NULL;
/**
- * Mutex to synchronize the callback to be called for the network
- * changes.
- */
-static oc_mutex g_bleNetworkCbMutex = NULL;
-
-/**
* Mutex to synchronize the updates of the local LE address of the
* adapter.
*/
static ca_thread_pool_t g_bleAdapterThreadPool = NULL;
/**
- * Mutex to synchronize the task to be pushed to thread pool.
- */
-static oc_mutex g_bleAdapterThreadPoolMutex = NULL;
-
-/**
- * Mutex to synchronize the queing of the data from SenderQueue.
- */
-static oc_mutex g_bleClientSendDataMutex = NULL;
-
-/**
* Mutex to synchronize the queing of the data from ReceiverQueue.
*/
static oc_mutex g_bleClientReceiveDataMutex = NULL;
/**
- * Mutex to synchronize the queing of the data from SenderQueue.
- */
-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 oc_mutex g_bleAdapterReqRespCbMutex = NULL;
-
-/**
* Callback to be called when network packet received from either
* GattServer or GattClient.
*/
static CAErrorHandleCallback g_errorHandler = NULL;
#ifdef __WITH_DTLS__
-static void CALESecureReceiveDataCB(const CASecureEndpoint_t *endpoint,
+static CAResult_t CALESecureReceiveDataCB(const CASecureEndpoint_t *endpoint,
const void *data, size_t dataLength);
static ssize_t CALESecureSendDataCB(CAEndpoint_t *endpoint,
static u_arraylist_t *g_bleClientSenderInfo = NULL;
/**
+ * Mutex to synchronize access to all senderInfos
+ */
+static oc_mutex g_senderInfoMutex = NULL;
+
+/**
* Queue to process the outgoing packets from GATTServer.
*/
static CAQueueingThread_t *g_bleServerSendQueueHandle = NULL;
* @param[in] address target address to remove data in queue.
*/
static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle,
- oc_mutex mutex,
const char* address);
/**
/**
* get received data info and positioned index from the received data list
* for client / server which is matched same leAddress and port.
+ * CABLESenderInfo_t is reference counted structure, you must call
+ * CALEFreeSenderInfo on senderInfo to release reference.
*
* @param[in] leAddress target address to get serderInfo.
- * @param[in] port target port to get serderInfo.
+ * @param[in] port target port to get senderInfo.
* @param[in] senderInfoList received data list for client / server.
* @param[out] senderInfo Pointer to contain matched(leAddress and port)
* received data info.
uint32_t *senderIndex);
/**
+ * Add sender info to list.
+ *
+ * @param[in] u_arraylist_t Array list to add sender info to.
+ * @param[in] senderInfo Sender info to be added to list.
+ *
+ * @return true on success, otherwise false.
+ */
+static bool CALEAddSenderInfoToList(u_arraylist_t *senderInfoList,
+ CABLESenderInfo_t *senderInfo);
+
+/**
+ * Remove desired sender info from list.
+ *
+ * @param[in] u_arraylist_t Array list to remove sender info from.
+ * @param[in] senderInfo Sender info to be removed from list.
+ *
+ */
+static void CALERemoveSenderInfoFromList(u_arraylist_t *senderInfoList,
+ CABLESenderInfo_t *senderInfo);
+
+/**
+ * Free sender info. CABLESenderInfo_t is reference counted structure
+ * and info will be freed only when reference count reaches 0 or negative.
+ *
+ * @param[in] senderInfo Sender info to be freed.
+ *
+ */
+static CAResult_t CALEFreeSenderInfo(CABLESenderInfo_t *senderInfo);
+
+/**
* get ports related to remote address. It is need because multi application
* can have more than 2 senderInfo using same BLE address. So before remove
* receive queue data, should get port list from sender Info.
static CAResult_t CAInitLEServerQueues()
{
- oc_mutex_lock(g_bleAdapterThreadPoolMutex);
-
CAResult_t result = CAInitLEServerSenderQueue();
if (CA_STATUS_OK != result)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
- oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
+
return CA_STATUS_FAILED;
}
if (!g_bleServerSenderInfo)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
- oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
return CA_MEMORY_ALLOC_FAILED;
}
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerReceiverQueue failed");
u_arraylist_free(&g_bleServerSenderInfo);
- oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
return CA_STATUS_FAILED;
}
g_dataBleServerReceiverHandlerState = true;
-
- oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
return CA_STATUS_OK;
}
static CAResult_t CAInitLEClientQueues()
{
- oc_mutex_lock(g_bleAdapterThreadPoolMutex);
-
CAResult_t result = CAInitLEClientSenderQueue();
if (CA_STATUS_OK != result)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
- oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
return CA_STATUS_FAILED;
}
if (!g_bleClientSenderInfo)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
- oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
return CA_MEMORY_ALLOC_FAILED;
}
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientReceiverQueue failed");
u_arraylist_free(&g_bleClientSenderInfo);
- oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
return CA_STATUS_FAILED;
}
g_dataBleClientReceiverHandlerState = true;
- oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
return CA_STATUS_OK;
}
return CA_STATUS_OK;
}
-static void CALEClearSenderInfoImpl(u_arraylist_t ** list)
+static void CALEClearSenderInfoImpl(u_arraylist_t **list)
{
+ oc_mutex_lock(g_senderInfoMutex);
const size_t length = u_arraylist_length(*list);
for (size_t i = 0; i < length; ++i)
{
}
}
u_arraylist_free(list);
+ oc_mutex_unlock(g_senderInfoMutex);
}
static void CALEClearSenderInfo()
"NULL index argument",
CA_STATUS_INVALID_PARAM);
+ oc_mutex_lock(g_senderInfoMutex);
const uint32_t listLength = u_arraylist_length(senderInfoList);
const uint32_t addrLength = strlen(leAddress);
for (uint32_t index = 0; index < listLength; index++)
continue;
}
- if (!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
+ if (!strncmp(info->remoteEndpoint->addr, leAddress, addrLength)
+ && info->remoteEndpoint->port == port)
{
- if (info->remoteEndpoint->port == port)
+ *senderIndex = index;
+ if (senderInfo)
{
- *senderIndex = index;
- if (senderInfo)
- {
- *senderInfo = info;
- }
- return CA_STATUS_OK;
+ *senderInfo = info;
+ (*senderInfo)->refCount++;
}
+ oc_mutex_unlock(g_senderInfoMutex);
+ return CA_STATUS_OK;
}
}
+ oc_mutex_unlock(g_senderInfoMutex);
return CA_STATUS_FAILED;
}
+static bool CALEAddSenderInfoToList(u_arraylist_t *senderInfoList, CABLESenderInfo_t *senderInfo)
+{
+ oc_mutex_lock(g_senderInfoMutex);
+ senderInfo->refCount++;
+ if (!u_arraylist_add(senderInfoList,(void *)senderInfo))
+ {
+ senderInfo->refCount--;
+ oc_mutex_unlock(g_senderInfoMutex);
+ return false;
+ }
+ oc_mutex_unlock(g_senderInfoMutex);
+ return true;
+}
+
+static void CALERemoveSenderInfoFromList(u_arraylist_t *senderInfoList, CABLESenderInfo_t *senderInfo)
+{
+ oc_mutex_lock(g_senderInfoMutex);
+ uint32_t idx;
+ bool elemFound = u_arraylist_get_index(senderInfoList, (const void*)senderInfo, &idx);
+ if(elemFound)
+ {
+ void *info = u_arraylist_remove(senderInfoList, idx);
+ if (info != NULL)
+ {
+ senderInfo->refCount--;
+ }
+ }
+ oc_mutex_unlock(g_senderInfoMutex);
+}
+
+static CAResult_t CALEFreeSenderInfo(CABLESenderInfo_t *senderInfo)
+{
+ VERIFY_NON_NULL_RET(senderInfo,
+ CALEADAPTER_TAG,
+ "NULL senderInfo to remove argument",
+ CA_STATUS_INVALID_PARAM);
+
+ oc_mutex_lock(g_senderInfoMutex);
+ senderInfo->refCount--;
+ if(senderInfo->refCount <= 0)
+ {
+ OICFree(senderInfo->defragData);
+ OICFree(senderInfo->remoteEndpoint);
+ OICFree(senderInfo);
+ }
+ oc_mutex_unlock(g_senderInfoMutex);
+ return CA_STATUS_OK;
+}
+
static void CALEDataReceiverHandler(void *threadData, CABLEAdapter_t receiverType)
{
OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__);
{
OIC_LOG(ERROR, CALEADAPTER_TAG,
"This packet is start packet but exist senderInfo. Remove senderInfo");
- u_arraylist_remove(bleData->senderInfo, senderIndex);
- OICFree(senderInfo->defragData);
- OICFree(senderInfo);
+ CALERemoveSenderInfoFromList(bleData->senderInfo, senderInfo);
+ CALEFreeSenderInfo(senderInfo);
senderInfo = NULL;
senderIndex = 0;
}
return;
}
- CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
+ CABLESenderInfo_t *newSender = OICCalloc(1, sizeof(CABLESenderInfo_t));
if (!newSender)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
oc_mutex_unlock(bleReceiveDataMutex);
return;
}
- newSender->recvDataLen = 0;
- newSender->totalDataLen = 0;
- newSender->defragData = NULL;
- newSender->remoteEndpoint = NULL;
OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
newSender->totalDataLen = totalLength;
-
if (!(newSender->totalDataLen))
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
- OICFree(newSender);
+ CALEFreeSenderInfo(newSender);
oc_mutex_unlock(bleReceiveDataMutex);
return;
}
if (NULL == newSender->defragData)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
- OICFree(newSender);
+ CALEFreeSenderInfo(newSender);
oc_mutex_unlock(bleReceiveDataMutex);
return;
}
if (NULL == newSender->remoteEndpoint)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
- OICFree(newSender->defragData);
- OICFree(newSender);
+ CALEFreeSenderInfo(newSender);
oc_mutex_unlock(bleReceiveDataMutex);
return;
}
if (newSender->recvDataLen + dataOnlyLen > newSender->totalDataLen)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "buffer is smaller than received data");
- OICFree(newSender->defragData);
- CAFreeEndpoint(newSender->remoteEndpoint);
- OICFree(newSender);
+ CALEFreeSenderInfo(newSender);
oc_mutex_unlock(bleReceiveDataMutex);
return;
}
dataOnlyLen);
newSender->recvDataLen += dataOnlyLen;
- u_arraylist_add(bleData->senderInfo,(void *)newSender);
+ newSender->refCount = 1;
- //Getting newSender index position in bleSenderInfo array list
- if (CA_STATUS_OK !=
- CALEGetSenderInfo(newSender->remoteEndpoint->addr,
- newSender->remoteEndpoint->port,
- bleData->senderInfo,
- NULL, &senderIndex))
+ if (!CALEAddSenderInfoToList(bleData->senderInfo, newSender))
{
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
- OICFree(newSender->defragData);
- CAFreeEndpoint(newSender->remoteEndpoint);
- OICFree(newSender);
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to add sender info!");
+ CALEFreeSenderInfo(newSender);
oc_mutex_unlock(bleReceiveDataMutex);
return;
}
+
senderInfo = newSender;
}
else
OIC_LOG_V(ERROR, CALEADAPTER_TAG,
"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);
+ CALERemoveSenderInfoFromList(bleData->senderInfo, senderInfo);
+ CALEFreeSenderInfo(senderInfo);
oc_mutex_unlock(bleReceiveDataMutex);
return;
}
if (senderInfo->totalDataLen == senderInfo->recvDataLen)
{
- oc_mutex_lock(g_bleAdapterReqRespCbMutex);
if (NULL == g_networkPacketReceivedCallback)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
-
- u_arraylist_remove(bleData->senderInfo, senderIndex);
- OICFree(senderInfo->defragData);
- OICFree(senderInfo);
- oc_mutex_unlock(g_bleAdapterReqRespCbMutex);
+ CALERemoveSenderInfoFromList(bleData->senderInfo, senderInfo);
+ CALEFreeSenderInfo(senderInfo);
oc_mutex_unlock(bleReceiveDataMutex);
return;
}
break;
default:
OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Unsupported rcvr type:%d",receiverType);
- oc_mutex_unlock(g_bleAdapterReqRespCbMutex);
- u_arraylist_remove(bleData->senderInfo, senderIndex);
+ CALERemoveSenderInfoFromList(bleData->senderInfo, senderInfo);
senderInfo->remoteEndpoint = NULL;
senderInfo->defragData = NULL;
- OICFree(senderInfo);
+ CALEFreeSenderInfo(senderInfo);
oc_mutex_unlock(bleReceiveDataMutex);
return;
}
}
#endif
- oc_mutex_unlock(g_bleAdapterReqRespCbMutex);
- u_arraylist_remove(bleData->senderInfo, senderIndex);
+ CALERemoveSenderInfoFromList(bleData->senderInfo, senderInfo);
senderInfo->remoteEndpoint = NULL;
senderInfo->defragData = NULL;
- OICFree(senderInfo);
+ CALEFreeSenderInfo(senderInfo);
+ oc_mutex_unlock(bleReceiveDataMutex);
+ return;
}
+ CALEFreeSenderInfo(senderInfo);
}
oc_mutex_unlock(bleReceiveDataMutex);
}
"CAGenerateVariableForFragmentation failed, result [%d]", result);
if (g_errorHandler)
{
- g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
- }
+ g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
+ }
return;
}
uint32_t dataLen = 0;
if (g_mtuSize > totalLength)
{
- length = totalLength;
+ length = (uint32_t)totalLength;
dataLen = bleData->dataLen;
}
else
OIC_LOG_V(DEBUG,
CALEADAPTER_TAG,
- "Server Sent Unicast First Data - data length [%zu]",
+ "Server Sent Unicast First Data - data length [%u]",
length);
result = CAGenerateHeader(dataHeader,
return;
}
-#if defined(__TIZEN__) || defined(__ANDROID__)
+#if defined(__ANDROID__)
// get MTU size
if (false == CALEClientIsConnected(bleData->remoteEndpoint->addr))
{
CALEADAPTER_TAG,
"CALEClientSendNegotiationMessage has failed, result [%d]",
res);
- if (g_errorHandler)
+ if (g_errorHandler)
{
g_errorHandler(bleData->remoteEndpoint,
bleData->data,
return;
}
}
+#endif
+#if defined(__TIZEN__) || defined(__ANDROID__)
g_mtuSize = CALEClientGetMtuSize(bleData->remoteEndpoint->addr);
#endif
OIC_LOG_V(INFO, CALEADAPTER_TAG, "MTU size [%d]", g_mtuSize);
uint32_t dataLen = 0;
if (g_mtuSize > totalLength)
{
- length = totalLength;
+ length = (uint32_t)totalLength;
dataLen = bleData->dataLen;
}
else
}
OIC_LOG_V(DEBUG,
CALEADAPTER_TAG,
- "Client Sent Unicast First Data - data length [%zu]",
+ "Client Sent Unicast First Data - data length [%u]",
length);
result = CAGenerateHeader(dataHeader,
CALEADAPTER_TAG,
"Update characteristics failed, result [%d]",
result);
- if (g_errorHandler)
+ if (g_errorHandler)
{
g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
}
}
OIC_LOG_V(DEBUG,
CALEADAPTER_TAG,
- "Client Sent Unicast %d Data - data(mtu) length [%zu]",
+ "Client Sent Unicast %d Data - data(mtu) length [%hu]",
index + 1,
g_mtuSize);
}
static CAResult_t CALEServerSendDataSingleThread(const uint8_t *data,
uint32_t dataLen)
{
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__););
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__);
VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
}
}
- if (NULL == g_bleNetworkCbMutex)
- {
- g_bleNetworkCbMutex = oc_mutex_new();
- if (NULL == g_bleNetworkCbMutex)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed");
- CATerminateLEAdapterMutex();
- return CA_STATUS_FAILED;
- }
- }
-
if (NULL == g_bleLocalAddressMutex)
{
g_bleLocalAddressMutex = oc_mutex_new();
}
}
- if (NULL == g_bleAdapterThreadPoolMutex)
- {
- g_bleAdapterThreadPoolMutex = oc_mutex_new();
- if (NULL == g_bleAdapterThreadPoolMutex)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed");
- CATerminateLEAdapterMutex();
- return CA_STATUS_FAILED;
- }
- }
-
- if (NULL == g_bleClientSendDataMutex)
- {
- g_bleClientSendDataMutex = oc_mutex_new();
- if (NULL == g_bleClientSendDataMutex)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed");
- CATerminateLEAdapterMutex();
- return CA_STATUS_FAILED;
- }
- }
-
- if (NULL == g_bleServerSendDataMutex)
- {
- g_bleServerSendDataMutex = oc_mutex_new();
- if (NULL == g_bleServerSendDataMutex)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed");
- CATerminateLEAdapterMutex();
- return CA_STATUS_FAILED;
- }
- }
+
- if (NULL == g_bleAdapterReqRespCbMutex)
+ if (NULL == g_bleServerReceiveDataMutex)
{
- g_bleAdapterReqRespCbMutex = oc_mutex_new();
- if (NULL == g_bleAdapterReqRespCbMutex)
+ g_bleServerReceiveDataMutex = oc_mutex_new();
+ if (NULL == g_bleServerReceiveDataMutex)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed");
- CATerminateLEAdapterMutex();
return CA_STATUS_FAILED;
}
}
- if (NULL == g_bleServerReceiveDataMutex)
+ if (NULL == g_bleClientReceiveDataMutex)
{
- g_bleServerReceiveDataMutex = oc_mutex_new();
- if (NULL == g_bleServerReceiveDataMutex)
+ g_bleClientReceiveDataMutex = oc_mutex_new();
+ if (NULL == g_bleClientReceiveDataMutex)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed");
return CA_STATUS_FAILED;
}
}
- if (NULL == g_bleClientReceiveDataMutex)
+ if (NULL == g_senderInfoMutex)
{
- g_bleClientReceiveDataMutex = oc_mutex_new();
- if (NULL == g_bleClientReceiveDataMutex)
+ g_senderInfoMutex = oc_mutex_new();
+ if (NULL == g_senderInfoMutex)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed");
return CA_STATUS_FAILED;
oc_mutex_free(g_bleIsServerMutex);
g_bleIsServerMutex = NULL;
- oc_mutex_free(g_bleNetworkCbMutex);
- g_bleNetworkCbMutex = NULL;
-
+
oc_mutex_free(g_bleLocalAddressMutex);
g_bleLocalAddressMutex = NULL;
- oc_mutex_free(g_bleAdapterThreadPoolMutex);
- g_bleAdapterThreadPoolMutex = NULL;
-
- oc_mutex_free(g_bleClientSendDataMutex);
- g_bleClientSendDataMutex = NULL;
-
- oc_mutex_free(g_bleServerSendDataMutex);
- g_bleServerSendDataMutex = NULL;
-
- oc_mutex_free(g_bleAdapterReqRespCbMutex);
- g_bleAdapterReqRespCbMutex = NULL;
-
oc_mutex_free(g_bleServerReceiveDataMutex);
g_bleServerReceiveDataMutex = NULL;
oc_mutex_free(g_bleClientReceiveDataMutex);
g_bleClientReceiveDataMutex = NULL;
+
+ oc_mutex_free(g_senderInfoMutex);
+ g_senderInfoMutex = NULL;
}
/**
static CAResult_t CALEAdapterGattServerStart()
{
OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__);
+ CAResult_t result = CA_STATUS_FAILED;
if (caglobals.bleFlags & CA_LE_SERVER_DISABLE)
{
return CA_STATUS_OK;
}
- CAResult_t result = CAStartLEGattServer();
+#ifndef DISABLE_BLE_SERVER
+ OIC_LOG_V(INFO, CALEADAPTER_TAG, "Starting LE GATT Server");
+ result = CAStartLEGattServer();
+#endif
#ifndef SINGLE_THREAD
/*
*/
if (CA_STATUS_OK == result)
{
- oc_mutex_lock(g_bleServerSendDataMutex);
result = CAQueueingThreadStart(g_bleServerSendQueueHandle);
- oc_mutex_unlock(g_bleServerSendDataMutex);
-
if (CA_STATUS_OK != result)
{
OIC_LOG_V(ERROR,
#ifndef SINGLE_THREAD
- oc_mutex_lock(g_bleServerSendDataMutex);
CAResult_t res = CAQueueingThreadStop(g_bleServerSendQueueHandle);
if (CA_STATUS_OK != res)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "CAQueueingThreadStop has failed");
}
- oc_mutex_unlock(g_bleServerSendDataMutex);
-
res = CAStopLEGattServer();
if (CA_STATUS_OK != res)
{
*/
if (CA_STATUS_OK == result)
{
- oc_mutex_lock(g_bleClientSendDataMutex);
result = CAQueueingThreadStart(g_bleClientSendQueueHandle);
- oc_mutex_unlock(g_bleClientSendDataMutex);
-
if (CA_STATUS_OK != result)
{
OIC_LOG(ERROR,
OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__);
CAStopLEGattClient();
- oc_mutex_lock(g_bleClientSendDataMutex);
CAResult_t result = CAQueueingThreadStop(g_bleClientSendQueueHandle);
- oc_mutex_unlock(g_bleClientSendDataMutex);
-
+
return result;
#else
CAStopLEGattClient();
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);
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "encrypted datalen = %zd", dataLen);
CAResult_t result;
CADataType_t dataType = g_dataType;
return ret;
}
-void CALESecureReceiveDataCB(const CASecureEndpoint_t *sep, const void *data,
+CAResult_t 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");
+ VERIFY_NON_NULL(sep, CALEADAPTER_TAG, "sep is NULL");
+ VERIFY_NON_NULL(data, CALEADAPTER_TAG, "data is NULL");
OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
- "Secure Data Receive - decrypted datalen = %d", dataLen);
+ "Secure Data Receive - decrypted datalen = %zd", dataLen);
if (dataLen <= 0)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "incorrect dataLen, derecypt fail !");
- return;
+ return CA_STATUS_INVALID_PARAM;
}
OIC_LOG_BUFFER(DEBUG, CALEADAPTER_TAG, data, dataLen);
+ CAResult_t res = CA_STATUS_OK;
if (g_networkPacketReceivedCallback)
{
OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
sep->endpoint.flags, sep->endpoint.flags);
OIC_LOG(DEBUG, CALEADAPTER_TAG,
"[CALESecureReceiveDataCB] Received data up !");
- g_networkPacketReceivedCallback(sep, data, dataLen);
+ res = g_networkPacketReceivedCallback(sep, data, dataLen);
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Error parsing CoAP data, res = %d", res);
+ }
}
+ return res;
}
#endif
static CAResult_t CAStopLE()
{
-#ifdef __WITH_DTLS__
- CAdeinitSslAdapter();
-#endif
#ifndef SINGLE_THREAD
CAStopLEQueues();
g_dataType = dataType;
oc_mutex_unlock(g_bleIsServerMutex);
- result = CAencryptSsl(endpoint, data, dataLen);
+ result = CAencryptSsl(endpoint, (void *)data, dataLen);
if (CA_STATUS_OK != result)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "caadapternetdtlsencrypt failed!");
g_dataType = dataType;
oc_mutex_unlock(g_bleIsServerMutex);
- result = CAencryptSsl(endpoint, data, dataLen);
+ result = CAencryptSsl(endpoint, (void *)data, dataLen);
if (CA_STATUS_OK != result)
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "caadapternetdtlsencrypt failed!");
{
OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__);
- oc_mutex_lock(g_bleNetworkCbMutex);
g_networkCallback = netCallback;
g_connectionCallback = connCallback;
- oc_mutex_unlock(g_bleNetworkCbMutex);
CAResult_t res = CA_STATUS_OK;
if (netCallback)
{
// remove data of send queue.
if (g_bleClientSendQueueHandle)
{
- CALERemoveSendQueueData(g_bleClientSendQueueHandle,
- g_bleClientSendDataMutex,
- address);
+ CALERemoveSendQueueData(g_bleClientSendQueueHandle, address);
}
if (g_bleServerSendQueueHandle)
{
- CALERemoveSendQueueData(g_bleServerSendQueueHandle,
- g_bleServerSendDataMutex,
- address);
+ CALERemoveSendQueueData(g_bleServerSendQueueHandle, address);
}
#endif
#ifdef __WITH_DTLS__
+#if defined(__TIZEN__) && !defined(SINGLE_THREAD)
+ // CAcloseSslConnection returns CAResult_t instead of void*, but the size is the same and crash shouldn't occur
+ pthread_t ccThread;
+ pthread_attr_t attr;
+ int initAttrRes = -1;
+ int pthreadCreateRes = -1;
+ int detachStatusRes = -1;
+ int memoryAllocationRes = -1;
+
+ do
+ {
+ initAttrRes = pthread_attr_init(&attr);
+ if (initAttrRes != 0)
+ {
+ break;
+ }
+ CAEndpoint_t *localEndpointCpyPtr = OICMalloc(sizeof(CAEndpoint_t));
+
+ if(NULL == localEndpointCpyPtr)
+ {
+ memoryAllocationRes = -1;
+ break;
+ }
+ else
+ {
+ memoryAllocationRes = 0;
+ }
+
+ (*localEndpointCpyPtr) = localEndpoint;
+ // this piece of code is reached on the main thread
+ // CAcloseSslConnection might wait for too long (network + mutexes) and watchdog might kill it
+ // Asynchronous call protects this function from watchdog
+ pthreadCreateRes = pthread_create(&ccThread, &attr, (void *(*)(void*))&CAcloseSslConnectionFreeEndpoint, (void*)localEndpointCpyPtr);
+
+ if (pthreadCreateRes != 0)
+ {
+ break;
+ }
+ detachStatusRes = pthread_detach(ccThread);
+ }while (0);
+
+ // regardless of CAcloseSslConnection result, the function will continue and g_connectionCallback will be called
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "CAcloseSslConnection pthread_init [%d], mem_alloc [%d] pthread_create [%d], pthread_detach [%d]",
+ initAttrRes, memoryAllocationRes, pthreadCreateRes, detachStatusRes);
+#else
CAcloseSslConnection(&localEndpoint);
#endif
+#endif
}
if (g_connectionCallback)
VERIFY_NON_NULL_RET(g_bleClientSendQueueHandle, CALEADAPTER_TAG,
"g_bleClientSendQueueHandle is NULL",
CA_STATUS_FAILED);
- VERIFY_NON_NULL_RET(g_bleClientSendDataMutex, CALEADAPTER_TAG,
- "g_bleClientSendDataMutex is NULL",
- CA_STATUS_FAILED);
-
+
OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%u]", dataLen);
CALEData_t *bleData = CACreateLEData(remoteEndpoint, data, dataLen, NULL);
return CA_MEMORY_ALLOC_FAILED;
}
// Add message to send queue
- oc_mutex_lock(g_bleClientSendDataMutex);
- CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData, sizeof(CALEData_t));
- oc_mutex_unlock(g_bleClientSendDataMutex);
+
+ CAResult_t res = CAQueueingThreadAddData(g_bleClientSendQueueHandle, bleData,
+ sizeof(CALEData_t));
+ if (CA_STATUS_OK != res)
+ {
+ CALEDataDestroyer(bleData, sizeof(CALEData_t));
+ }
#endif
return CA_STATUS_OK;
}
}
#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_bleServerSendQueueHandle, CALEADAPTER_TAG, "sendQueueHandle",
+ "g_bleServerSendQueueHandle is NULL",
CA_STATUS_FAILED);
OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Data Sending to LE layer [%d]", dataLen);
}
// Add message to send queue
- oc_mutex_lock(g_bleServerSendDataMutex);
- CAQueueingThreadAddData(g_bleServerSendQueueHandle,
- bleData,
- sizeof(CALEData_t));
- oc_mutex_unlock(g_bleServerSendDataMutex);
+
+ CAResult_t res = CAQueueingThreadAddData(g_bleServerSendQueueHandle,
+ bleData,
+ sizeof(CALEData_t));
+ if (CA_STATUS_OK != res)
+ {
+ CALEDataDestroyer(bleData, sizeof(CALEData_t));
+ }
#endif
OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__);
return CA_STATUS_OK;
// Create bleData to add to queue
OIC_LOG_V(DEBUG,
CALEADAPTER_TAG,
- "Data received from LE Client layer [%zu]",
+ "Data received from LE Client layer [%u]",
dataLength);
CALEData_t * const bleData =
static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle)
{
- oc_mutex_lock(g_bleAdapterThreadPoolMutex);
g_bleAdapterThreadPool = handle;
- oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
+
}
static void CASetLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback)
{
- oc_mutex_lock(g_bleAdapterReqRespCbMutex);
-
g_networkPacketReceivedCallback = callback;
- oc_mutex_unlock(g_bleAdapterReqRespCbMutex);
}
static void CALEErrorHandler(const char *remoteAddress,
}
#ifndef SINGLE_THREAD
-static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle, oc_mutex mutex,
- const char* address)
+static bool CALEClearQueueAddressDataContext(void *data, uint32_t size, void *ctx)
+{
+ if (NULL == data || NULL == ctx)
+ {
+ return false;
+ }
+
+ CALEData_t *caLeData = (CALEData_t *)data;
+ const char *address = (const char *)ctx;
+
+ if (NULL != caLeData && NULL != caLeData->remoteEndpoint)
+ {
+ if (!strcasecmp(caLeData->remoteEndpoint->addr, address))
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
+static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle, const char* address)
{
OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__);
VERIFY_NON_NULL_VOID(queueHandle, CALEADAPTER_TAG, "queueHandle");
VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
- oc_mutex_lock(mutex);
- while (u_queue_get_size(queueHandle->dataQueue) > 0)
+ CAResult_t res = CAQueueingThreadClearContextData(queueHandle,
+ CALEClearQueueAddressDataContext,
+ address);
+ if (CA_STATUS_OK != res)
{
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "get data from queue");
- u_queue_message_t *message = u_queue_get_element(queueHandle->dataQueue);
- if (NULL != message)
- {
- CALEData_t *bleData = (CALEData_t *) message->msg;
- if (bleData && bleData->remoteEndpoint)
- {
- if (!strcasecmp(bleData->remoteEndpoint->addr, address))
- {
- OIC_LOG(DEBUG, CALEADAPTER_TAG, "found the message of disconnected device");
- if (NULL != queueHandle->destroy)
- {
- queueHandle->destroy(message->msg, message->size);
- }
- else
- {
- OICFree(message->msg);
- }
-
- OICFree(message);
- }
- }
- }
+
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Could not clear the send queue");
}
- oc_mutex_unlock(mutex);
+
}
static void CALERemoveReceiveQueueData(u_arraylist_t *dataInfoList, const char* address)
dataInfoList, &senderInfo,
&senderIndex))
{
- u_arraylist_remove(dataInfoList, senderIndex);
- OICFree(senderInfo->defragData);
- OICFree(senderInfo->remoteEndpoint);
- OICFree(senderInfo);
-
OIC_LOG(DEBUG, CALEADAPTER_TAG,
"SenderInfo is removed for disconnection");
+ CALERemoveSenderInfoFromList(dataInfoList, senderInfo);
+ CALEFreeSenderInfo(senderInfo);
}
else
{