#include "caadapterutils.h"
#include "cainterfacecontroller.h"
#include "caretransmission.h"
+#include "oic_string.h"
#ifdef WITH_BWT
#include "cablockwisetransfer.h"
#else
#define CA_MAX_RT_ARRAY_SIZE 3
-#endif /* SINGLE_THREAD */
+#endif // SINGLE_THREAD
-#define TAG "CA_MSG_HNDLR"
+#define TAG "OIC_CA_MSG_HANDLE"
static CARetransmission_t g_retransmissionContext;
static CARequestCallback g_requestHandler = NULL;
static CAResponseCallback g_responseHandler = NULL;
static CAErrorCallback g_errorHandler = NULL;
+static CANetworkMonitorCallback g_nwMonitorHandler = NULL;
static void CAErrorHandler(const CAEndpoint_t *endpoint,
const void *data, uint32_t dataLen,
#endif
static void CADestroyData(void *data, uint32_t size);
static void CALogPayloadInfo(CAInfo_t *info);
-static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *endpoint, uint16_t id);
+static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *endpoint, uint16_t id,
+ CAToken_t token, uint8_t tokenLength);
#ifdef WITH_BWT
void CAAddDataToSendThread(CAData_t *data)
{
- OIC_LOG(DEBUG, TAG, "IN");
VERIFY_NON_NULL_VOID(data, TAG, "data");
// add thread
CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
-
- OIC_LOG(DEBUG, TAG, "OUT");
}
void CAAddDataToReceiveThread(CAData_t *data)
{
- OIC_LOG(DEBUG, TAG, "IN - CAAddDataToReceiveThread");
VERIFY_NON_NULL_VOID(data, TAG, "data");
// add thread
CAQueueingThreadAddData(&g_receiveThread, data, sizeof(CAData_t));
-
- OIC_LOG(DEBUG, TAG, "OUT - CAAddDataToReceiveThread");
}
#endif
static bool CAIsSelectedNetworkAvailable()
{
u_arraylist_t *list = CAGetSelectedNetworkList();
- if (!list || list->length == 0)
+ if (!list || u_arraylist_length(list) == 0)
{
OIC_LOG(ERROR, TAG, "No selected network");
return false;
if (!ep)
{
OIC_LOG(ERROR, TAG, "endpoint clone failed");
- OICFree(cadata);
- return NULL;
+ goto exit;
}
OIC_LOG_V(DEBUG, TAG, "address : %s", ep->addr);
- CAResult_t result;
- if(CA_RESPONSE_DATA == dataType)
+ if (CA_RESPONSE_DATA == dataType)
{
CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
if (!resInfo)
{
OIC_LOG(ERROR, TAG, "memory allocation failed");
- OICFree(cadata);
- CAFreeEndpoint(ep);
- return NULL;
+ goto exit;
}
- result = CAGetResponseInfoFromPDU(data, resInfo);
+ CAResult_t result = CAGetResponseInfoFromPDU(data, resInfo, endpoint);
if (CA_STATUS_OK != result)
{
OIC_LOG(ERROR, TAG, "CAGetResponseInfoFromPDU Failed");
- CAFreeEndpoint(ep);
CADestroyResponseInfoInternal(resInfo);
- OICFree(cadata);
- return NULL;
+ goto exit;
}
cadata->responseInfo = resInfo;
info = &resInfo->info;
if (!reqInfo)
{
OIC_LOG(ERROR, TAG, "memory allocation failed");
- OICFree(cadata);
- CAFreeEndpoint(ep);
- return NULL;
+ goto exit;
}
- result = CAGetRequestInfoFromPDU(data, reqInfo);
+ CAResult_t result = CAGetRequestInfoFromPDU(data, endpoint, reqInfo);
if (CA_STATUS_OK != result)
{
OIC_LOG(ERROR, TAG, "CAGetRequestInfoFromPDU failed");
- CAFreeEndpoint(ep);
CADestroyRequestInfoInternal(reqInfo);
- OICFree(cadata);
- return NULL;
+ goto exit;
}
- if (CADropSecondMessage(&caglobals.ca.requestHistory, endpoint, reqInfo->info.messageId))
+ if (CADropSecondMessage(&caglobals.ca.requestHistory, endpoint, reqInfo->info.messageId,
+ reqInfo->info.token, reqInfo->info.tokenLength))
{
OIC_LOG(ERROR, TAG, "Second Request with same Token, Drop it");
- CAFreeEndpoint(ep);
CADestroyRequestInfoInternal(reqInfo);
- OICFree(cadata);
- return NULL;
+ goto exit;
}
cadata->requestInfo = reqInfo;
if (!errorInfo)
{
OIC_LOG(ERROR, TAG, "Memory allocation failed!");
- OICFree(cadata);
- CAFreeEndpoint(ep);
- return NULL;
+ goto exit;
}
- CAResult_t result = CAGetErrorInfoFromPDU(data, errorInfo);
+ CAResult_t result = CAGetErrorInfoFromPDU(data, endpoint, errorInfo);
if (CA_STATUS_OK != result)
{
OIC_LOG(ERROR, TAG, "CAGetErrorInfoFromPDU failed");
- CAFreeEndpoint(ep);
OICFree(errorInfo);
- OICFree(cadata);
- return NULL;
+ goto exit;
}
cadata->errorInfo = errorInfo;
cadata->remoteEndpoint = ep;
cadata->dataType = dataType;
+ OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData OUT");
return cadata;
- OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData OUT");
+exit:
+ OICFree(cadata);
+ CAFreeEndpoint(ep);
+ return NULL;
}
static void CATimeoutCallback(const CAEndpoint_t *endpoint, const void *pdu, uint32_t size)
{
- OIC_LOG(DEBUG, TAG, "IN");
VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
resInfo->info.type = CAGetMessageTypeFromPduBinaryData(pdu, size);
resInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
- CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *) pdu, &(resInfo->info));
+ CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *) pdu, &(resInfo->info),
+ endpoint);
if (CA_STATUS_OK != res)
{
OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
cadata->requestInfo = NULL;
cadata->responseInfo = resInfo;
+#ifdef WITH_BWT
+ if (CAIsSupportedBlockwiseTransfer(endpoint->adapter))
+ {
+ res = CARemoveBlockDataFromListWithSeed(resInfo->info.token, resInfo->info.tokenLength,
+ endpoint->port);
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "CARemoveBlockDataFromListWithSeed failed");
+ }
+ }
+#endif // WITH_BWT
+
#ifdef SINGLE_THREAD
CAProcessReceivedData(cadata);
#else
CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
#endif
-
- OIC_LOG(DEBUG, TAG, "OUT");
}
static void CADestroyData(void *data, uint32_t size)
CADestroyErrorInfoInternal(cadata->errorInfo);
}
- OICFree(cadata->options);
OICFree(cadata);
OIC_LOG(DEBUG, TAG, "CADestroyData OUT");
}
g_errorHandler(rep, data->errorInfo);
}
-#ifdef SINGLE_THREAD
CADestroyData(data, sizeof(CAData_t));
-#endif
OIC_LOG(DEBUG, TAG, "CAProcessReceivedData OUT");
}
#endif
#ifndef SINGLE_THREAD
-
static void CAReceiveThreadProcess(void *threadData)
{
- OIC_LOG(DEBUG, TAG, "IN");
#ifndef SINGLE_HANDLE
CAData_t *data = (CAData_t *) threadData;
CAProcessReceivedData(data);
#else
(void)threadData;
#endif
- OIC_LOG(DEBUG, TAG, "OUT");
}
-#endif
+#endif // SINGLE_THREAD
+
+static CAResult_t CAProcessMulticastData(const CAData_t *data)
+{
+ VERIFY_NON_NULL(data, TAG, "data");
+ VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");
+
+ coap_pdu_t *pdu = NULL;
+ CAInfo_t *info = NULL;
+ coap_list_t *options = NULL;
+ coap_transport_type transport = coap_udp;
+ CAResult_t res = CA_SEND_FAILED;
+ if (NULL != data->requestInfo)
+ {
+ OIC_LOG(DEBUG, TAG, "requestInfo is available..");
+
+ info = &data->requestInfo->info;
+ pdu = CAGeneratePDU(CA_GET, info, data->remoteEndpoint, &options, &transport);
+
+ if (NULL != pdu)
+ {
+#ifdef WITH_BWT
+ if (CAIsSupportedBlockwiseTransfer(data->remoteEndpoint->adapter))
+ {
+ // Blockwise transfer
+ res = CAAddBlockOption(&pdu, info, data->remoteEndpoint, &options);
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG(DEBUG, TAG, "CAAddBlockOption has failed");
+ goto exit;
+ }
+ }
+#endif // WITH_BWT
+ }
+ else
+ {
+ OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
+ CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
+ return res;
+ }
+ }
+ else
+ {
+ OIC_LOG(ERROR, TAG, "not supported message type for multicast.");
+ return res;
+ }
+
+ CALogPDUInfo(pdu, data->remoteEndpoint);
+
+ OIC_LOG(DEBUG, TAG, "pdu to send :");
+ OIC_LOG_BUFFER(DEBUG, TAG, (uint8_t*)pdu->hdr, pdu->length);
+
+ res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
+ goto exit;
+ }
+
+ coap_delete_list(options);
+ coap_delete_pdu(pdu);
+ return res;
+
+exit:
+ CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
+ coap_delete_list(options);
+ coap_delete_pdu(pdu);
+ return res;
+}
static CAResult_t CAProcessSendData(const CAData_t *data)
{
- OIC_LOG(DEBUG, TAG, "IN");
VERIFY_NON_NULL(data, TAG, "data");
VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");
coap_pdu_t *pdu = NULL;
CAInfo_t *info = NULL;
+ coap_list_t *options = NULL;
+ coap_transport_type transport = coap_udp;
if (SEND_TYPE_UNICAST == type)
{
-
OIC_LOG(DEBUG,TAG,"Unicast message");
+
+#ifdef ROUTING_GATEWAY
+ /*
+ * When forwarding a packet, do not attempt retransmission as its the responsibility of
+ * packet originator node
+ */
+ bool skipRetransmission = false;
+#endif
+
if (NULL != data->requestInfo)
{
OIC_LOG(DEBUG, TAG, "requestInfo is available..");
info = &data->requestInfo->info;
- pdu = CAGeneratePDU(data->requestInfo->method, info, data->remoteEndpoint);
+#ifdef ROUTING_GATEWAY
+ skipRetransmission = data->requestInfo->info.skipRetransmission;
+#endif
+ pdu = CAGeneratePDU(data->requestInfo->method, info, data->remoteEndpoint,
+ &options, &transport);
}
else if (NULL != data->responseInfo)
{
OIC_LOG(DEBUG, TAG, "responseInfo is available..");
info = &data->responseInfo->info;
- pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint);
+#ifdef ROUTING_GATEWAY
+ skipRetransmission = data->responseInfo->info.skipRetransmission;
+#endif
+ pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint,
+ &options, &transport);
}
else
{
if (NULL != pdu)
{
#ifdef WITH_BWT
- if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter)
+ if (CAIsSupportedBlockwiseTransfer(data->remoteEndpoint->adapter))
{
// Blockwise transfer
if (NULL != info)
{
- CAResult_t res = CAAddBlockOption(&pdu, *info,
- data->remoteEndpoint);
+ CAResult_t res = CAAddBlockOption(&pdu, info,
+ data->remoteEndpoint,
+ &options);
if (CA_STATUS_OK != res)
{
OIC_LOG(INFO, TAG, "to write block option has failed");
CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
+ coap_delete_list(options);
coap_delete_pdu(pdu);
return res;
}
}
}
-#endif
- CALogPDUInfo(pdu);
+#endif // WITH_BWT
+ CALogPDUInfo(pdu, data->remoteEndpoint);
res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
if (CA_STATUS_OK != res)
{
OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
+ coap_delete_list(options);
coap_delete_pdu(pdu);
return res;
}
- // for retransmission
- res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
- pdu->length);
- if ((CA_STATUS_OK != res) && (CA_NOT_SUPPORTED != res))
+
+#ifdef WITH_TCP
+ if (CAIsSupportedCoAPOverTCP(data->remoteEndpoint->adapter))
{
- //when retransmission not supported this will return CA_NOT_SUPPORTED, ignore
- OIC_LOG_V(INFO, TAG, "retransmission is not enabled due to error, res : %d", res);
- coap_delete_pdu(pdu);
- return res;
+ OIC_LOG(INFO, TAG, "retransmission will be not worked");
+ }
+ else
+#endif
+#ifdef ROUTING_GATEWAY
+ if(!skipRetransmission)
+#endif
+ {
+ // for retransmission
+ res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint,
+ pdu->hdr, pdu->length);
+ if ((CA_STATUS_OK != res) && (CA_NOT_SUPPORTED != res))
+ {
+ //when retransmission not supported this will return CA_NOT_SUPPORTED, ignore
+ OIC_LOG_V(INFO, TAG, "retransmission is not enabled due to error, res : %d", res);
+ coap_delete_list(options);
+ coap_delete_pdu(pdu);
+ return res;
+ }
}
+ coap_delete_list(options);
coap_delete_pdu(pdu);
}
else
else if (SEND_TYPE_MULTICAST == type)
{
OIC_LOG(DEBUG,TAG,"Multicast message");
- if (NULL != data->requestInfo)
+#ifdef WITH_TCP
+ /*
+ * If CoAP over TCP is enabled, the CoAP pdu wont be same for IP and other adapters.
+ * That's why we need to generate two pdu's, one for IP and second for other transports.
+ * Two possible cases we might have to split: a) when adapter is CA_DEFAULT_ADAPTER
+ * b) when one of the adapter is IP adapter(ex: CA_ADAPTER_IP | CA_ADAPTER_GATT_BTLE)
+ */
+ if (data->remoteEndpoint->adapter == CA_DEFAULT_ADAPTER ||
+ (CA_ADAPTER_IP & data->remoteEndpoint->adapter &&
+ CA_ADAPTER_IP != data->remoteEndpoint->adapter))
{
- OIC_LOG(DEBUG, TAG, "requestInfo is available..");
-
- info = &data->requestInfo->info;
- pdu = CAGeneratePDU(CA_GET, info, data->remoteEndpoint);
- if (NULL != pdu)
+ if (data->remoteEndpoint->adapter == CA_DEFAULT_ADAPTER)
{
-#ifdef WITH_BWT
- if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter)
- {
- // Blockwise transfer
- CAResult_t res = CAAddBlockOption(&pdu, data->requestInfo->info,
- data->remoteEndpoint);
- if (CA_STATUS_OK != res)
- {
- OIC_LOG(DEBUG, TAG, "CAAddBlockOption has failed");
- CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
- coap_delete_pdu(pdu);
- return res;
- }
- }
-#endif
- CALogPDUInfo(pdu);
-
- res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
- if (CA_STATUS_OK != res)
- {
- OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
- CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
- coap_delete_pdu(pdu);
- return res;
- }
-
- coap_delete_pdu(pdu);
+ data->remoteEndpoint->adapter = CA_ALL_ADAPTERS ^ CA_ADAPTER_IP;
}
else
{
- OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
- CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
- return CA_SEND_FAILED;
+ data->remoteEndpoint->adapter = data->remoteEndpoint->adapter ^ CA_ADAPTER_IP;
}
+ CAProcessMulticastData(data);
+ data->remoteEndpoint->adapter = CA_ADAPTER_IP;
+ CAProcessMulticastData(data);
}
else
{
- OIC_LOG(ERROR, TAG, "request info is empty");
- return CA_SEND_FAILED;
+ CAProcessMulticastData(data);
}
+#else
+ CAProcessMulticastData(data);
+#endif
}
- OIC_LOG(DEBUG, TAG, "OUT");
return CA_STATUS_OK;
}
CAData_t *data = (CAData_t *) threadData;
CAProcessSendData(data);
}
-
#endif
/*
- * If a second message arrives with the same token and the other address
+ * If a second message arrives with the same message ID, token and the other address
* family, drop it. Typically, IPv6 beats IPv4, so the IPv4 message is dropped.
*/
-static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *ep, uint16_t id)
+static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *ep, uint16_t id,
+ CAToken_t token, uint8_t tokenLength)
{
if (!ep)
{
return false;
}
+ if (tokenLength > CA_MAX_TOKEN_LEN)
+ {
+ /*
+ * If token length is more than CA_MAX_TOKEN_LEN,
+ * we compare the first CA_MAX_TOKEN_LEN bytes only.
+ */
+ tokenLength = CA_MAX_TOKEN_LEN;
+ }
+
bool ret = false;
CATransportFlags_t familyFlags = ep->flags & CA_IPFAMILY_MASK;
- for (int i = 0; i < sizeof(history->items) / sizeof(history->items[0]); i++)
+ for (size_t i = 0; i < sizeof(history->items) / sizeof(history->items[0]); i++)
{
CAHistoryItem_t *item = &(history->items[i]);
- if (id == item->messageId)
+ if (id == item->messageId && tokenLength == item->tokenLength
+ && memcmp(item->token, token, tokenLength) == 0)
{
if ((familyFlags ^ item->flags) == CA_IPFAMILY_MASK)
{
OIC_LOG_V(INFO, TAG, "IPv%c duplicate message ignored",
- familyFlags & CA_IPV6 ? '6' : '4');
+ familyFlags & CA_IPV6 ? '6' : '4');
ret = true;
break;
}
history->items[history->nextIndex].flags = familyFlags;
history->items[history->nextIndex].messageId = id;
+ if (token && tokenLength)
+ {
+ memcpy(history->items[history->nextIndex].token, token, tokenLength);
+ history->items[history->nextIndex].tokenLength = tokenLength;
+ }
+
if (++history->nextIndex >= HISTORYSIZE)
{
history->nextIndex = 0;
static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
const void *data, uint32_t dataLen)
{
- OIC_LOG(DEBUG, TAG, "IN");
VERIFY_NON_NULL_VOID(sep, TAG, "remoteEndpoint");
VERIFY_NON_NULL_VOID(data, TAG, "data");
+ OIC_LOG(DEBUG, TAG, "received pdu data :");
+ OIC_LOG_BUFFER(DEBUG, TAG, data, dataLen);
+
uint32_t code = CA_NOT_FOUND;
CAData_t *cadata = NULL;
- coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
+ coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code,
+ &(sep->endpoint));
if (NULL == pdu)
{
OIC_LOG(ERROR, TAG, "Parse PDU failed");
return;
}
- // for retransmission
- void *retransmissionPdu = NULL;
- CARetransmissionReceivedData(&g_retransmissionContext, cadata->remoteEndpoint, pdu->hdr,
- pdu->length, &retransmissionPdu);
-
- // get token from saved data in retransmission list
- if (retransmissionPdu && CA_EMPTY == code)
+#ifdef WITH_TCP
+ if (CAIsSupportedCoAPOverTCP(sep->endpoint.adapter))
+ {
+ OIC_LOG(INFO, TAG, "retransmission is not supported");
+ }
+ else
+#endif
{
- if (cadata->responseInfo)
+ // for retransmission
+ void *retransmissionPdu = NULL;
+ CARetransmissionReceivedData(&g_retransmissionContext, cadata->remoteEndpoint, pdu->hdr,
+ pdu->length, &retransmissionPdu);
+
+ // get token from saved data in retransmission list
+ if (retransmissionPdu && CA_EMPTY == code)
{
- CAInfo_t *info = &cadata->responseInfo->info;
- CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
- info);
- if (CA_STATUS_OK != res)
+ if (cadata->responseInfo)
{
- OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
- OICFree(info->token);
- info->tokenLength = 0;
+ CAInfo_t *info = &cadata->responseInfo->info;
+ CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
+ info, &(sep->endpoint));
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
+ OICFree(info->token);
+ info->tokenLength = 0;
+ }
}
}
+ OICFree(retransmissionPdu);
}
- OICFree(retransmissionPdu);
}
cadata->type = SEND_TYPE_UNICAST;
CAProcessReceivedData(cadata);
#else
#ifdef WITH_BWT
- if (CA_ADAPTER_GATT_BTLE != sep->endpoint.adapter)
+ if (CAIsSupportedBlockwiseTransfer(sep->endpoint.adapter))
{
CAResult_t res = CAReceiveBlockWiseData(pdu, &(sep->endpoint), cadata, dataLen);
- if (CA_NOT_SUPPORTED == res)
+ if (CA_NOT_SUPPORTED == res || CA_REQUEST_TIMEOUT == res)
{
- OIC_LOG(ERROR, TAG, "this message does not have block option");
+ OIC_LOG(DEBUG, TAG, "this message does not have block option");
CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
}
else
{
CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
}
-#endif
+#endif // SINGLE_THREAD
coap_delete_pdu(pdu);
-
- OIC_LOG(DEBUG, TAG, "OUT");
-}
-
-static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
-{
- (void)info;
- (void)status;
- OIC_LOG(DEBUG, TAG, "IN");
-
- OIC_LOG(DEBUG, TAG, "OUT");
}
void CAHandleRequestResponseCallbacks()
ca_mutex_unlock(g_receiveThread.threadMutex);
- if (NULL == item)
- {
- return;
- }
-
- // get values
- void *msg = item->msg;
-
- if (NULL == msg)
+ if (NULL == item || NULL == item->msg)
{
return;
}
// get endpoint
- CAData_t *td = (CAData_t *) msg;
+ CAData_t *td = (CAData_t *) item->msg;
if (td->requestInfo && g_requestHandler)
{
g_errorHandler(td->remoteEndpoint, td->errorInfo);
}
- CADestroyData(msg, sizeof(CAData_t));
+ CADestroyData(item->msg, sizeof(CAData_t));
OICFree(item);
-#endif /* SINGLE_HANDLE */
-#endif
+#endif // SINGLE_HANDLE
+#endif // SINGLE_THREAD
}
static CAData_t* CAPrepareSendData(const CAEndpoint_t *endpoint, const void *sendData,
CADataType_t dataType)
{
OIC_LOG(DEBUG, TAG, "CAPrepareSendData IN");
- CAInfo_t *info = NULL;
CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
if (!cadata)
return NULL;
}
- if(CA_REQUEST_DATA == dataType)
+ if (CA_REQUEST_DATA == dataType)
{
// clone request info
CARequestInfo_t *request = CACloneRequestInfo((CARequestInfo_t *)sendData);
- if(!request)
+ if (!request)
{
OIC_LOG(ERROR, TAG, "CACloneRequestInfo failed");
- OICFree(cadata);
- return NULL;
+ goto exit;
}
cadata->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
- info = &request->info;
cadata->requestInfo = request;
}
- else if(CA_RESPONSE_DATA == dataType)
+ else if (CA_RESPONSE_DATA == dataType)
{
// clone response info
CAResponseInfo_t *response = CACloneResponseInfo((CAResponseInfo_t *)sendData);
if(!response)
{
OIC_LOG(ERROR, TAG, "CACloneResponseInfo failed");
- OICFree(cadata);
- return NULL;
+ goto exit;
}
- cadata->type = SEND_TYPE_UNICAST;
- info = &response->info;
+ cadata->type = response->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
cadata->responseInfo = response;
}
else
{
OIC_LOG(ERROR, TAG, "CAPrepareSendData unknown data type");
- OICFree(cadata);
- return NULL;
- }
-
- if (NULL != info->options && 0 < info->numOptions)
- {
- uint8_t numOptions = info->numOptions;
- // copy data
- CAHeaderOption_t *headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t)
- * numOptions);
- if(!headerOption)
- {
- OIC_LOG(ERROR, TAG, "memory allocation failed");
- CADestroyData(cadata, sizeof(CAData_t));
- return NULL;
- }
-
- memcpy(headerOption, info->options, sizeof(CAHeaderOption_t) * numOptions);
-
- cadata->options = headerOption;
- cadata->numOptions = numOptions;
+ goto exit;
}
CAEndpoint_t* ep = CACloneEndpoint(endpoint);
if (!ep)
{
OIC_LOG(ERROR, TAG, "endpoint clone failed");
- CADestroyData(cadata, sizeof(CAData_t));
- return NULL;
+ goto exit;
}
cadata->remoteEndpoint = ep;
cadata->dataType = dataType;
return cadata;
+
+exit:
+ CADestroyData(cadata, sizeof(CAData_t));
+ return NULL;
}
-CAResult_t CADetachRequestMessage(const CAEndpoint_t *object, const CARequestInfo_t *request)
+CAResult_t CADetachSendMessage(const CAEndpoint_t *endpoint, const void *sendMsg,
+ CADataType_t dataType)
{
- OIC_LOG(DEBUG, TAG, "IN");
-
- VERIFY_NON_NULL(object, TAG, "object");
- VERIFY_NON_NULL(request, TAG, "request");
+ VERIFY_NON_NULL(endpoint, TAG, "endpoint");
+ VERIFY_NON_NULL(sendMsg, TAG, "sendMsg");
if (false == CAIsSelectedNetworkAvailable())
{
OIC_LOG(ERROR, TAG, "max RT queue size reached!");
return CA_SEND_FAILED;
}
-#endif /* ARDUINO */
-
- CAData_t *data = CAPrepareSendData(object, request, CA_REQUEST_DATA);
- if(!data)
- {
- OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
- return CA_MEMORY_ALLOC_FAILED;
- }
-
-#ifdef SINGLE_THREAD
- CAResult_t result = CAProcessSendData(data);
- if(CA_STATUS_OK != result)
- {
- OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
- return result;
- }
-
- CADestroyData(data, sizeof(CAData_t));
-#else
-#ifdef WITH_BWT
- if (CA_ADAPTER_GATT_BTLE != object->adapter)
- {
- // send block data
- CAResult_t res = CASendBlockWiseData(data);
- if(CA_NOT_SUPPORTED == res)
- {
- OIC_LOG(DEBUG, TAG, "normal msg will be sent");
- CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
- return CA_STATUS_OK;
- }
- else
- {
- CADestroyData(data, sizeof(CAData_t));
- }
- return res;
- }
- else
-#endif
- {
- CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
- }
-#endif
-
- OIC_LOG(DEBUG, TAG, "OUT");
- return CA_STATUS_OK;
-}
-
-CAResult_t CADetachResponseMessage(const CAEndpoint_t *object,
- const CAResponseInfo_t *response)
-{
- OIC_LOG(DEBUG, TAG, "IN");
- VERIFY_NON_NULL(object, TAG, "object");
- VERIFY_NON_NULL(response, TAG, "response");
-
- if (false == CAIsSelectedNetworkAvailable())
- {
- return CA_STATUS_FAILED;
- }
+#endif // ARDUINO
- CAData_t *data = CAPrepareSendData(object, response, CA_RESPONSE_DATA);
+ CAData_t *data = CAPrepareSendData(endpoint, sendMsg, dataType);
if(!data)
{
OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
#ifdef SINGLE_THREAD
CAResult_t result = CAProcessSendData(data);
- if(result != CA_STATUS_OK)
+ if (CA_STATUS_OK != result)
{
OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
+ CADestroyData(data, sizeof(CAData_t));
return result;
}
CADestroyData(data, sizeof(CAData_t));
#else
#ifdef WITH_BWT
- if (CA_ADAPTER_GATT_BTLE != object->adapter)
+ if (CAIsSupportedBlockwiseTransfer(endpoint->adapter))
{
// send block data
CAResult_t res = CASendBlockWiseData(data);
- if(CA_NOT_SUPPORTED == res)
+ if (CA_NOT_SUPPORTED == res)
{
OIC_LOG(DEBUG, TAG, "normal msg will be sent");
CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
return res;
}
else
-#endif
+#endif // WITH_BWT
{
CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
}
-#endif
+#endif // SINGLE_THREAD
- OIC_LOG(DEBUG, TAG, "OUT");
return CA_STATUS_OK;
}
-CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
- uint8_t tokenLength, const CAHeaderOption_t *options,
- uint8_t numOptions)
-{
- (void)resourceUri;
- (void)token;
- (void)tokenLength;
- (void)options;
- (void)numOptions;
- return CA_NOT_SUPPORTED;
-}
-
void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
CAErrorCallback errorHandler)
{
- OIC_LOG(DEBUG, TAG, "IN");
g_requestHandler = ReqHandler;
g_responseHandler = RespHandler;
g_errorHandler = errorHandler;
- OIC_LOG(DEBUG, TAG, "OUT");
+}
+
+void CASetNetworkMonitorCallback(CANetworkMonitorCallback nwMonitorHandler)
+{
+ g_nwMonitorHandler = nwMonitorHandler;
}
CAResult_t CAInitializeMessageHandler()
{
- OIC_LOG(DEBUG, TAG, "IN");
CASetPacketReceivedCallback(CAReceivedPacketCallback);
-
- CASetNetworkChangeCallback(CANetworkChangedCallback);
CASetErrorHandleCallback(CAErrorHandler);
#ifndef SINGLE_THREAD
// create thread pool
CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
-
if (CA_STATUS_OK != res)
{
OIC_LOG(ERROR, TAG, "thread pool initialize error.");
}
// send thread initialize
- if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
- CASendThreadProcess, CADestroyData))
+ res = CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
+ CASendThreadProcess, CADestroyData);
+ if (CA_STATUS_OK != res)
{
OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
- return CA_STATUS_FAILED;
+ ca_thread_pool_free(g_threadPoolHandle);
+ g_threadPoolHandle = NULL;
+ return res;
}
// start send thread
res = CAQueueingThreadStart(&g_sendThread);
-
if (CA_STATUS_OK != res)
{
OIC_LOG(ERROR, TAG, "thread start error(send thread).");
ca_thread_pool_free(g_threadPoolHandle);
g_threadPoolHandle = NULL;
+ CAQueueingThreadDestroy(&g_sendThread);
return res;
}
// receive thread initialize
- if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
- CAReceiveThreadProcess, CADestroyData))
+ res = CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
+ CAReceiveThreadProcess, CADestroyData);
+ if (CA_STATUS_OK != res)
{
OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
- return CA_STATUS_FAILED;
+ ca_thread_pool_free(g_threadPoolHandle);
+ g_threadPoolHandle = NULL;
+ CAQueueingThreadDestroy(&g_sendThread);
+ return res;
}
#ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
// start receive thread
- res = CAQueueingThreadStart(&gReceiveThread);
-
- if (res != CA_STATUS_OK)
+ res = CAQueueingThreadStart(&g_receiveThread);
+ if (CA_STATUS_OK != res)
{
OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
+ ca_thread_pool_free(g_threadPoolHandle);
+ g_threadPoolHandle = NULL;
+ CAQueueingThreadDestroy(&g_sendThread);
+ CAQueueingThreadDestroy(&g_receiveThread);
return res;
}
-#endif /* SINGLE_HANDLE */
+#endif // SINGLE_HANDLE
// retransmission initialize
- CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle, CASendUnicastData,
- CATimeoutCallback, NULL);
+ res = CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle,
+ CASendUnicastData, CATimeoutCallback, NULL);
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "Failed to Initialize Retransmission.");
+ ca_thread_pool_free(g_threadPoolHandle);
+ g_threadPoolHandle = NULL;
+ CAQueueingThreadDestroy(&g_sendThread);
+ CAQueueingThreadDestroy(&g_receiveThread);
+ return res;
+ }
#ifdef WITH_BWT
// block-wise transfer initialize
- CAInitializeBlockWiseTransfer(CAAddDataToSendThread, CAAddDataToReceiveThread);
+ res = CAInitializeBlockWiseTransfer(CAAddDataToSendThread, CAAddDataToReceiveThread);
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "Failed to Initialize BlockWiseTransfer.");
+ ca_thread_pool_free(g_threadPoolHandle);
+ g_threadPoolHandle = NULL;
+ CAQueueingThreadDestroy(&g_sendThread);
+ CAQueueingThreadDestroy(&g_receiveThread);
+ CARetransmissionDestroy(&g_retransmissionContext);
+ return res;
+ }
#endif
// start retransmission
res = CARetransmissionStart(&g_retransmissionContext);
-
if (CA_STATUS_OK != res)
{
OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
+ ca_thread_pool_free(g_threadPoolHandle);
+ g_threadPoolHandle = NULL;
+ CAQueueingThreadDestroy(&g_sendThread);
+ CAQueueingThreadDestroy(&g_receiveThread);
+ CARetransmissionDestroy(&g_retransmissionContext);
return res;
}
CAInitializeAdapters(g_threadPoolHandle);
#else
// retransmission initialize
- CARetransmissionInitialize(&g_retransmissionContext, NULL, CASendUnicastData,
- CATimeoutCallback, NULL);
+ CAResult_t res = CARetransmissionInitialize(&g_retransmissionContext, NULL, CASendUnicastData,
+ CATimeoutCallback, NULL);
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "Failed to Initialize Retransmission.");
+ return res;
+ }
+
CAInitializeAdapters();
-#endif
+#endif // SINGLE_THREAD
- OIC_LOG(DEBUG, TAG, "OUT");
return CA_STATUS_OK;
}
void CATerminateMessageHandler()
{
- OIC_LOG(DEBUG, TAG, "IN");
#ifndef SINGLE_THREAD
CATransportAdapter_t connType;
u_arraylist_t *list = CAGetSelectedNetworkList();
if (NULL != g_receiveThread.threadMutex)
{
#ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
- CAQueueingThreadStop(&gReceiveThread);
-#endif /* SINGLE_HANDLE */
+ CAQueueingThreadStop(&g_receiveThread);
+#endif
}
// destroy thread pool
// stop retransmission
CARetransmissionStop(&g_retransmissionContext);
CARetransmissionDestroy(&g_retransmissionContext);
-#endif
-
- OIC_LOG(DEBUG, TAG, "OUT");
+#endif // SINGLE_THREAD
}
-void CALogPDUInfo(coap_pdu_t *pdu)
+void CALogPDUInfo(coap_pdu_t *pdu, const CAEndpoint_t *endpoint)
{
VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
+ VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
- OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
+#ifdef WITH_TCP
+ if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
+ {
+ OIC_LOG(DEBUG, TAG, "pdu header data :");
+ OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) pdu->hdr, pdu->length);
+ }
+ else
+#endif
+ {
+ OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->coap_hdr_udp_t.type);
- OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
+ OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->coap_hdr_udp_t.code);
- OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
+ OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
- OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);
+ OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->coap_hdr_udp_t.token,
+ pdu->hdr->coap_hdr_udp_t.token_length);
+ }
}
static void CALogPayloadInfo(CAInfo_t *info)
{
- if(info)
+ if (info)
{
if (info->options)
{
for (uint32_t i = 0; i < info->numOptions; i++)
{
- OIC_LOG_V(DEBUG, TAG, "optionID: %d", info->options[i].optionID);
+ OIC_LOG_V(DEBUG, TAG, "optionID: %u", info->options[i].optionID);
OIC_LOG_V(DEBUG, TAG, "list: %s", info->options[i].optionData);
}
if (info->payload)
{
- OIC_LOG_V(DEBUG, TAG, "payload: %p(%u)", info->payload,
+ OIC_LOG_V(DEBUG, TAG, "payload: %p(%zu)", info->payload,
info->payloadSize);
}
OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) info->token,
info->tokenLength);
}
- OIC_LOG_V(DEBUG, TAG, "msgID: %d", info->messageId);
+ OIC_LOG_V(DEBUG, TAG, "msgID: %u", info->messageId);
}
else
{
OIC_LOG(DEBUG, TAG, "CAErrorHandler IN");
#ifndef SINGLE_THREAD
-
VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
VERIFY_NON_NULL_VOID(data, TAG, "data");
uint32_t code = CA_NOT_FOUND;
//Do not free remoteEndpoint and data. Currently they will be freed in data thread
//Get PDU data
- coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code);
+ coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code, endpoint);
if (NULL == pdu)
{
OIC_LOG(ERROR, TAG, "Parse PDU failed");
}
CAData_t *cadata = CAGenerateHandlerData(endpoint, NULL, pdu, CA_ERROR_DATA);
- if(!cadata)
+ if (!cadata)
{
OIC_LOG(ERROR, TAG, "CAErrorHandler, CAGenerateHandlerData failed!");
coap_delete_pdu(pdu);
CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
if (!cadata)
{
- OIC_LOG(ERROR, TAG, "memory allocation failed");
+ OIC_LOG(ERROR, TAG, "cadata memory allocation failed");
return;
}
CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
if (!errorInfo)
{
+ OIC_LOG(ERROR, TAG, "errorInfo memory allocation failed");
OICFree(cadata);
CAFreeEndpoint(ep);
return;
CAResult_t res = CACloneInfo(info, &errorInfo->info);
if (CA_STATUS_OK != res)
{
+ OIC_LOG(ERROR, TAG, "info clone failed");
OICFree(cadata);
+ OICFree(errorInfo);
CAFreeEndpoint(ep);
return;
}