X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fcsdk%2Fconnectivity%2Fsrc%2Fcamessagehandler.c;h=da4320d306813aada6fe63a87c02eb52c0ada32b;hb=32806a4cfb76b08b7a455cb6c3978d2a7a808675;hp=e2eb29557f02eba272e166c3e8abcd6d14734aed;hpb=839d642b7610d4d66ad464bb57de77b07d2ca59c;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/csdk/connectivity/src/camessagehandler.c b/resource/csdk/connectivity/src/camessagehandler.c index e2eb295..da4320d 100644 --- a/resource/csdk/connectivity/src/camessagehandler.c +++ b/resource/csdk/connectivity/src/camessagehandler.c @@ -28,12 +28,16 @@ #include "caremotehandler.h" #include "caprotocolmessage.h" #include "logger.h" -#include "config.h" /* for coap protocol */ +#include "trace.h" +#ifndef WITH_UPSTREAM_LIBCOAP +#include "coap/config.h" +#endif #include "oic_malloc.h" #include "canetworkconfigurator.h" #include "caadapterutils.h" #include "cainterfacecontroller.h" #include "caretransmission.h" +#include "oic_string.h" #ifdef WITH_BWT #include "cablockwisetransfer.h" @@ -47,6 +51,8 @@ #define SINGLE_HANDLE #define MAX_THREAD_POOL_SIZE 20 +#define UNUSED(x) (void)(x) + // thread pool handle static ca_thread_pool_t g_threadPoolHandle = NULL; @@ -54,6 +60,10 @@ static ca_thread_pool_t g_threadPoolHandle = NULL; static CAQueueingThread_t g_sendThread; static CAQueueingThread_t g_receiveThread; +#ifdef WITH_PROCESS_EVENT +static oc_event g_processEvent = NULL; +#endif // WITH_PROCESS_EVENT + #else #define CA_MAX_RT_ARRAY_SIZE 3 #endif // SINGLE_THREAD @@ -87,13 +97,31 @@ static void CALogPayloadInfo(CAInfo_t *info); static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *endpoint, uint16_t id, CAToken_t token, uint8_t tokenLength); +/** + * print send / receive message of CoAP. + * @param[in] data CA information which has send/receive message and endpoint. + * @param[in] pdu CoAP pdu low data. + */ +static void CALogPDUInfo(const CAData_t *data, const coap_pdu_t *pdu); + +#ifndef ARDUINO +static char g_headerBuffer[MAX_LOG_BUFFER_SIZE] = {0}; +static size_t g_headerIndex = 0; +static void CASamsungLogMessage(const CAData_t *data, const coap_pdu_t *pdu); +#endif + #ifdef WITH_BWT void CAAddDataToSendThread(CAData_t *data) { VERIFY_NON_NULL_VOID(data, TAG, "data"); // add thread - CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t)); + CAResult_t result = CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t), true); + if (result != CA_STATUS_OK) + { + OIC_LOG(ERROR, TAG, "Failed to add message to data queue!"); + CADestroyData(data, sizeof(CAData_t)); + } } void CAAddDataToReceiveThread(CAData_t *data) @@ -101,7 +129,20 @@ void CAAddDataToReceiveThread(CAData_t *data) VERIFY_NON_NULL_VOID(data, TAG, "data"); // add thread - CAQueueingThreadAddData(&g_receiveThread, data, sizeof(CAData_t)); + CAResult_t result = CAQueueingThreadAddData(&g_receiveThread, data, sizeof(CAData_t), false); + if (result != CA_STATUS_OK) + { + OIC_LOG(ERROR, TAG, "Failed to add message to data queue!"); + CADestroyData(data, sizeof(CAData_t)); + return; + } + +#ifdef WITH_PROCESS_EVENT + if (g_processEvent) + { + oc_event_signal(g_processEvent); + } +#endif } #endif @@ -129,37 +170,34 @@ static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint, OIC_LOG(ERROR, TAG, "memory allocation failed"); return NULL; } - +#ifdef SINGLE_THREAD + CAEndpoint_t* ep = endpoint; +#else CAEndpoint_t* ep = CACloneEndpoint(endpoint); if (!ep) { OIC_LOG(ERROR, TAG, "endpoint clone failed"); - OICFree(cadata); - return NULL; + goto exit; } +#endif 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, endpoint); + 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; @@ -176,29 +214,24 @@ static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint, if (!reqInfo) { OIC_LOG(ERROR, TAG, "memory allocation failed"); - OICFree(cadata); - CAFreeEndpoint(ep); - return NULL; + goto exit; } - result = CAGetRequestInfoFromPDU(data, endpoint, 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 ((reqInfo->info.type != CA_MSG_CONFIRM) && + 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); + OIC_LOG(INFO, TAG, "Second Request with same Token, Drop it"); CADestroyRequestInfoInternal(reqInfo); - OICFree(cadata); - return NULL; + goto exit; } cadata->requestInfo = reqInfo; @@ -216,19 +249,15 @@ static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint, if (!errorInfo) { OIC_LOG(ERROR, TAG, "Memory allocation failed!"); - OICFree(cadata); - CAFreeEndpoint(ep); - return NULL; + goto exit; } 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; @@ -246,26 +275,38 @@ static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint, OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData OUT"); return cadata; + +exit: + OICFree(cadata); +#ifndef SINGLE_THREAD + CAFreeEndpoint(ep); +#endif + return NULL; } static void CATimeoutCallback(const CAEndpoint_t *endpoint, const void *pdu, uint32_t size) { VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint"); VERIFY_NON_NULL_VOID(pdu, TAG, "pdu"); - +#ifdef SINGLE_THREAD + CAEndpoint_t* ep = endpoint; +#else CAEndpoint_t* ep = CACloneEndpoint(endpoint); if (!ep) { OIC_LOG(ERROR, TAG, "clone failed"); return; } +#endif CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t)); if (!resInfo) { OIC_LOG(ERROR, TAG, "calloc failed"); +#ifndef SINGLE_THREAD CAFreeEndpoint(ep); +#endif return; } @@ -273,13 +314,15 @@ static void CATimeoutCallback(const CAEndpoint_t *endpoint, const void *pdu, uin 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_transport_t *) pdu, &(resInfo->info), endpoint); if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list"); CADestroyResponseInfoInternal(resInfo); +#ifndef SINGLE_THREAD CAFreeEndpoint(ep); +#endif return; } @@ -287,7 +330,9 @@ static void CATimeoutCallback(const CAEndpoint_t *endpoint, const void *pdu, uin if (NULL == cadata) { OIC_LOG(ERROR, TAG, "memory allocation failed !"); +#ifndef SINGLE_THREAD CAFreeEndpoint(ep); +#endif CADestroyResponseInfoInternal(resInfo); return; } @@ -297,11 +342,36 @@ static void CATimeoutCallback(const CAEndpoint_t *endpoint, const void *pdu, uin 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 + CAResult_t result = CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t), false); + if (result != CA_STATUS_OK) + { + OIC_LOG(ERROR, TAG, "Failed to add message to data queue!"); + CADestroyData(cadata, sizeof(CAData_t)); + return; + } + +#ifdef WITH_PROCESS_EVENT + if (g_processEvent) + { + oc_event_signal(g_processEvent); + } +#endif//WITH_PROCESS_EVENT +#endif// SINGLE_THREAD } static void CADestroyData(void *data, uint32_t size) @@ -318,11 +388,12 @@ static void CADestroyData(void *data, uint32_t size) OIC_LOG(ERROR, TAG, "cadata is NULL"); return; } - +#ifndef SINGLE_THREAD if (NULL != cadata->remoteEndpoint) { CAFreeEndpoint(cadata->remoteEndpoint); } +#endif if (NULL != cadata->requestInfo) { @@ -387,7 +458,9 @@ static void CAReceiveThreadProcess(void *threadData) { #ifndef SINGLE_HANDLE CAData_t *data = (CAData_t *) threadData; + OIC_TRACE_BEGIN(%s:CAProcessReceivedData, TAG); CAProcessReceivedData(data); + OIC_TRACE_END(); #else (void)threadData; #endif @@ -402,82 +475,55 @@ static CAResult_t CAProcessMulticastData(const CAData_t *data) coap_pdu_t *pdu = NULL; CAInfo_t *info = NULL; coap_list_t *options = NULL; - coap_transport_type transport; + coap_transport_t transport = COAP_UDP; CAResult_t res = CA_SEND_FAILED; - if (NULL != data->requestInfo) + + if (!data->requestInfo && !data->responseInfo) + { + OIC_LOG(ERROR, TAG, "request or response info is empty"); + return res; + } + + if (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 if (NULL != data->responseInfo) + else if (data->responseInfo) { OIC_LOG(DEBUG, TAG, "responseInfo is available.."); info = &data->responseInfo->info; pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint, &options, &transport); - - if (NULL != pdu) - { -#ifdef WITH_BWT - if (CAIsSupportedBlockwiseTransfer(data->remoteEndpoint->adapter)) - { - // Blockwise transfer - if (NULL != info) - { - res = CAAddBlockOption(&pdu, info, data->remoteEndpoint, &options); - if (CA_STATUS_OK != res) - { - OIC_LOG(INFO, TAG, "to write block option 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 + + if (!pdu) { - OIC_LOG(ERROR, TAG, "request or response info is empty"); + OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU"); + CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED); + coap_delete_list(options); return res; } - CALogPDUInfo(pdu, data->remoteEndpoint); +#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 - OIC_LOG(DEBUG, TAG, "pdu to send :"); - OIC_LOG_BUFFER(DEBUG, TAG, (uint8_t*)pdu->hdr, pdu->length); + CALogPDUInfo(data, pdu); - res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length); + res = CASendMulticastData(data->remoteEndpoint, pdu->transport_hdr, pdu->length, data->dataType); if (CA_STATUS_OK != res) { OIC_LOG_V(ERROR, TAG, "send failed:%d", res); @@ -489,7 +535,7 @@ static CAResult_t CAProcessMulticastData(const CAData_t *data) return res; exit: - CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res); + CAErrorHandler(data->remoteEndpoint, pdu->transport_hdr, pdu->length, res); coap_delete_list(options); coap_delete_pdu(pdu); return res; @@ -502,16 +548,32 @@ static CAResult_t CAProcessSendData(const CAData_t *data) CAResult_t res = CA_STATUS_FAILED; + if (CA_NETWORK_COMMAND == data->dataType) + { + if (CA_REQ_DISCONNECT == data->eventInfo) + { +#ifdef TCP_ADAPTER + // request TCP disconnect + if (CA_ADAPTER_TCP == data->remoteEndpoint->adapter) + { + OIC_LOG(INFO, TAG, "request TCP disconnect"); + return CADisconnectSession(data->remoteEndpoint); + } +#endif + } + } + CASendDataType_t type = data->type; coap_pdu_t *pdu = NULL; CAInfo_t *info = NULL; coap_list_t *options = NULL; - coap_transport_type transport; + coap_transport_t 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 @@ -563,7 +625,7 @@ static CAResult_t CAProcessSendData(const CAData_t *data) if (CA_STATUS_OK != res) { OIC_LOG(INFO, TAG, "to write block option has failed"); - CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res); + CAErrorHandler(data->remoteEndpoint, pdu->transport_hdr, pdu->length, res); coap_delete_list(options); coap_delete_pdu(pdu); return res; @@ -571,13 +633,14 @@ static CAResult_t CAProcessSendData(const CAData_t *data) } } #endif // WITH_BWT - CALogPDUInfo(pdu, data->remoteEndpoint); + CALogPDUInfo(data, pdu); - res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length); + OIC_LOG_V(INFO, TAG, "CASendUnicastData type : %d", data->dataType); + res = CASendUnicastData(data->remoteEndpoint, pdu->transport_hdr, pdu->length, data->dataType); if (CA_STATUS_OK != res) { OIC_LOG_V(ERROR, TAG, "send failed:%d", res); - CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res); + CAErrorHandler(data->remoteEndpoint, pdu->transport_hdr, pdu->length, res); coap_delete_list(options); coap_delete_pdu(pdu); return res; @@ -591,12 +654,14 @@ static CAResult_t CAProcessSendData(const CAData_t *data) else #endif #ifdef ROUTING_GATEWAY - if(!skipRetransmission) + if (!skipRetransmission) #endif { // for retransmission - res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, - pdu->hdr, pdu->length); + res = CARetransmissionSentData(&g_retransmissionContext, + data->remoteEndpoint, + data->dataType, + pdu->transport_hdr, pdu->length); if ((CA_STATUS_OK != res) && (CA_NOT_SUPPORTED != res)) { //when retransmission not supported this will return CA_NOT_SUPPORTED, ignore @@ -651,7 +716,6 @@ static CAResult_t CAProcessSendData(const CAData_t *data) CAProcessMulticastData(data); #endif } - return CA_STATUS_OK; } @@ -659,7 +723,9 @@ static CAResult_t CAProcessSendData(const CAData_t *data) static void CASendThreadProcess(void *threadData) { CAData_t *data = (CAData_t *) threadData; + OIC_TRACE_BEGIN(%s:CAProcessSendData, TAG); CAProcessSendData(data); + OIC_TRACE_END(); } #endif @@ -682,7 +748,10 @@ static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *ep, ui { return false; } - + if (!history) + { + return false; + } if (tokenLength > CA_MAX_TOKEN_LEN) { /* @@ -727,15 +796,33 @@ static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *ep, ui return ret; } -static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep, - const void *data, uint32_t dataLen) +static CAResult_t CAReceivedPacketCallback(const CASecureEndpoint_t *sep, + const void *data, uint32_t dataLen) { - VERIFY_NON_NULL_VOID(sep, TAG, "remoteEndpoint"); - VERIFY_NON_NULL_VOID(data, TAG, "data"); + VERIFY_NON_NULL(sep, TAG, "remoteEndpoint"); + VERIFY_NON_NULL(data, TAG, "data"); + OIC_TRACE_BEGIN(%s:CAReceivedPacketCallback, TAG); + + if (0 == dataLen) + { + OIC_LOG(ERROR, TAG, "dataLen is zero"); + OIC_TRACE_END(); + return CA_STATUS_FAILED; + } + + // samsung log OIC_LOG(DEBUG, TAG, "received pdu data :"); - OIC_LOG_BUFFER(DEBUG, TAG, data, dataLen); + if (dataLen < 32) + { + OIC_LOG_BUFFER(DEBUG, TAG, data, dataLen); + } + else + { + OIC_LOG_BUFFER(DEBUG, TAG, data, 32); + } + CAResult_t res = CA_STATUS_OK; uint32_t code = CA_NOT_FOUND; CAData_t *cadata = NULL; @@ -744,7 +831,8 @@ static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep, if (NULL == pdu) { OIC_LOG(ERROR, TAG, "Parse PDU failed"); - return; + res = CA_STATUS_FAILED; + goto exit; } OIC_LOG_V(DEBUG, TAG, "code = %d", code); @@ -755,7 +843,7 @@ static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep, { OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!"); coap_delete_pdu(pdu); - return; + goto exit; } } else @@ -765,7 +853,7 @@ static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep, { OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!"); coap_delete_pdu(pdu); - return; + goto exit; } #ifdef WITH_TCP @@ -778,7 +866,7 @@ static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep, { // for retransmission void *retransmissionPdu = NULL; - CARetransmissionReceivedData(&g_retransmissionContext, cadata->remoteEndpoint, pdu->hdr, + CARetransmissionReceivedData(&g_retransmissionContext, cadata->remoteEndpoint, pdu->transport_hdr, pdu->length, &retransmissionPdu); // get token from saved data in retransmission list @@ -787,12 +875,13 @@ static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep, if (cadata->responseInfo) { CAInfo_t *info = &cadata->responseInfo->info; - CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu, + CAResult_t res = CAGetTokenFromPDU((const coap_hdr_transport_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->token = NULL; info->tokenLength = 0; } } @@ -803,6 +892,8 @@ static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep, cadata->type = SEND_TYPE_UNICAST; + CALogPDUInfo(cadata, pdu); + #ifdef SINGLE_THREAD CAProcessReceivedData(cadata); #else @@ -812,8 +903,8 @@ static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep, CAResult_t res = CAReceiveBlockWiseData(pdu, &(sep->endpoint), cadata, dataLen); if (CA_NOT_SUPPORTED == res || CA_REQUEST_TIMEOUT == res) { - OIC_LOG(ERROR, TAG, "this message does not have block option"); - CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t)); + OIC_LOG(DEBUG, TAG, "this message does not have block option"); + CAAddDataToReceiveThread(cadata); } else { @@ -823,24 +914,89 @@ static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep, else #endif { - CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t)); + CAResult_t result = CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t), false); + if (result != CA_STATUS_OK) + { + OIC_LOG(ERROR, TAG, "Failed to add message to data queue!"); + CADestroyData(cadata, sizeof(CAData_t)); + coap_delete_pdu(pdu); + goto exit; + } + +#ifdef WITH_PROCESS_EVENT + if (g_processEvent) + { + oc_event_signal(g_processEvent); + } +#endif } #endif // SINGLE_THREAD coap_delete_pdu(pdu); + +exit: + OIC_LOG(DEBUG, TAG, "OUT - Recv Thread"); + OIC_TRACE_END(); + return res; +} + +static void CAAdapterStateChangedCallback(CATransportAdapter_t transportType, bool enabled) +{ + if (!enabled) + { + CAClearMessageHandler(transportType); + } } -static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status) +static bool CAClearQueueEndpointDataContext(void *data, uint32_t size, void *ctx) { - (void)info; - (void)status; + UNUSED(size); + if (NULL == data || NULL == ctx) + { + return false; + } + + CAData_t *caData = (CAData_t *)data; + const CAEndpoint_t *endpoint = (const CAEndpoint_t *)ctx; - if (g_nwMonitorHandler) + if (NULL != caData && NULL != caData->remoteEndpoint) { - g_nwMonitorHandler(info, status); + if (strcmp(caData->remoteEndpoint->addr, endpoint->addr) == 0 + && caData->remoteEndpoint->port == endpoint->port + && caData->remoteEndpoint->adapter == endpoint->adapter) + { + return true; + } + } + return false; +} + +static void CAConnectionStateChangedCallback(const CAEndpoint_t *info, bool isConnected) +{ + if (!isConnected) + { + CAResult_t res = CAQueueingThreadClearContextData(&g_sendThread, + CAClearQueueEndpointDataContext, + (void *)info); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "Could not clear the send queue"); + } } } +static u_queue_message_t *get_receive_queue_item(void) +{ + u_queue_message_t *item = NULL; + + oc_mutex_lock(g_receiveThread.threadMutex); + item = u_queue_get_element(g_receiveThread.dataQueue); + oc_mutex_unlock(g_receiveThread.threadMutex); + + return item; +} + + void CAHandleRequestResponseCallbacks() { #ifdef SINGLE_THREAD @@ -852,39 +1008,44 @@ void CAHandleRequestResponseCallbacks() // #1 parse the data // #2 get endpoint - ca_mutex_lock(g_receiveThread.threadMutex); - - u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue); + u_queue_message_t *item = NULL; +#ifdef WITH_PROCESS_EVENT + while ((item = get_receive_queue_item()) != NULL) +#else + if ((item = get_receive_queue_item()) != NULL) +#endif + { if (NULL == item->msg) + { + OICFree(item); +#ifdef WITH_PROCESS_EVENT + continue; +#else + return; +#endif + } - ca_mutex_unlock(g_receiveThread.threadMutex); + // get endpoint + CAData_t *td = (CAData_t *) item->msg; - if (NULL == item || NULL == item->msg) - { - return; - } - - // get endpoint - CAData_t *td = (CAData_t *) item->msg; + if (td->requestInfo && g_requestHandler) + { + OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions); + g_requestHandler(td->remoteEndpoint, td->requestInfo); + } + else if (td->responseInfo && g_responseHandler) + { + OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions); + g_responseHandler(td->remoteEndpoint, td->responseInfo); + } + else if (td->errorInfo && g_errorHandler) + { + OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result); + g_errorHandler(td->remoteEndpoint, td->errorInfo); + } - if (td->requestInfo && g_requestHandler) - { - OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions); - g_requestHandler(td->remoteEndpoint, td->requestInfo); - } - else if (td->responseInfo && g_responseHandler) - { - OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions); - g_responseHandler(td->remoteEndpoint, td->responseInfo); + CADestroyData(item->msg, sizeof(CAData_t)); + OICFree(item); } - else if (td->errorInfo && g_errorHandler) - { - OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result); - g_errorHandler(td->remoteEndpoint, td->errorInfo); - } - - CADestroyData(item->msg, sizeof(CAData_t)); - OICFree(item); - #endif // SINGLE_HANDLE #endif // SINGLE_THREAD } @@ -901,31 +1062,35 @@ static CAData_t* CAPrepareSendData(const CAEndpoint_t *endpoint, const void *sen return NULL; } - if(CA_REQUEST_DATA == dataType) + if (CA_REQUEST_DATA == dataType) { +#ifdef SINGLE_THREAD + CARequestInfo_t *request = (CARequestInfo_t *)sendData; +#else // clone request info CARequestInfo_t *request = CACloneRequestInfo((CARequestInfo_t *)sendData); - - if(!request) + if (!request) { OIC_LOG(ERROR, TAG, "CACloneRequestInfo failed"); goto exit; } - +#endif cadata->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST; cadata->requestInfo = request; } - else if(CA_RESPONSE_DATA == dataType) + else if (CA_RESPONSE_DATA == dataType || CA_RESPONSE_FOR_RES == dataType) { +#ifdef SINGLE_THREAD + CAResponseInfo_t *response = (CAResponseInfo_t *)sendData; +#else // clone response info CAResponseInfo_t *response = CACloneResponseInfo((CAResponseInfo_t *)sendData); - - if(!response) + if (!response) { OIC_LOG(ERROR, TAG, "CACloneResponseInfo failed"); goto exit; } - +#endif cadata->type = response->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST; cadata->responseInfo = response; } @@ -935,23 +1100,72 @@ static CAData_t* CAPrepareSendData(const CAEndpoint_t *endpoint, const void *sen goto exit; } +#ifdef SINGLE_THREAD + CAEndpoint_t* ep = endpoint; +#else CAEndpoint_t* ep = CACloneEndpoint(endpoint); if (!ep) { OIC_LOG(ERROR, TAG, "endpoint clone failed"); - CADestroyData(cadata, sizeof(CAData_t)); - return NULL; + goto exit; } - +#endif cadata->remoteEndpoint = ep; cadata->dataType = dataType; return cadata; exit: +#ifndef SINGLE_THREAD + CADestroyData(cadata, sizeof(CAData_t)); +#else OICFree(cadata); +#endif return NULL; } +CAResult_t CADetachSendNetworkReqMessage(const CAEndpoint_t *endpoint, + CAConnectEvent_t event, + CADataType_t dataType) +{ + VERIFY_NON_NULL(endpoint, TAG, "endpoint"); + + if (false == CAIsSelectedNetworkAvailable()) + { + return CA_STATUS_FAILED; + } + +#ifndef SINGLE_THREAD + CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t)); + if (!cadata) + { + OIC_LOG(ERROR, TAG, "cadata memory allocation failed"); + return CA_MEMORY_ALLOC_FAILED; + } + + CAEndpoint_t* ep = CACloneEndpoint(endpoint); + if (!ep) + { + OIC_LOG(ERROR, TAG, "endpoint clone failed"); + OICFree(cadata); + return CA_MEMORY_ALLOC_FAILED; + } + + cadata->remoteEndpoint = ep; + cadata->eventInfo = event; + cadata->dataType = dataType; + + CAResult_t result = CAQueueingThreadAddData(&g_sendThread, cadata, sizeof(CAData_t), true); + if (result != CA_STATUS_OK) + { + OIC_LOG(ERROR, TAG, "Failed to add message to data queue!"); + CADestroyData(cadata, sizeof(CAData_t)); + return result; + } +#endif + + return CA_STATUS_OK; +} + CAResult_t CADetachSendMessage(const CAEndpoint_t *endpoint, const void *sendMsg, CADataType_t dataType) { @@ -979,38 +1193,55 @@ CAResult_t CADetachSendMessage(const CAEndpoint_t *endpoint, const void *sendMsg return CA_MEMORY_ALLOC_FAILED; } + OIC_LOG_V(INFO_PRIVATE, TAG, "DID of endpoint of this message is %s", endpoint->remoteId); + #ifdef SINGLE_THREAD CAResult_t result = CAProcessSendData(data); - if(CA_STATUS_OK != result) + if (CA_STATUS_OK != result) { OIC_LOG(ERROR, TAG, "CAProcessSendData failed"); - CADestroyData(data, sizeof(CAData_t)); + OICFree(data); return result; } - CADestroyData(data, sizeof(CAData_t)); + OICFree(data); + #else #ifdef WITH_BWT if (CAIsSupportedBlockwiseTransfer(endpoint->adapter)) { + CACheckAndDeleteTimedOutBlockData(); // 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)); + CAResult_t result = CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t), true); + if (result != CA_STATUS_OK) + { + OIC_LOG(ERROR, TAG, "Failed to add message to data queue!"); + CADestroyData(data, sizeof(CAData_t)); + return result; + } return CA_STATUS_OK; } else { CADestroyData(data, sizeof(CAData_t)); } + return res; } else #endif // WITH_BWT { - CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t)); + CAResult_t result = CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t), true); + if (result != CA_STATUS_OK) + { + OIC_LOG(ERROR, TAG, "Failed to add message to data queue!"); + CADestroyData(data, sizeof(CAData_t)); + return result; + } } #endif // SINGLE_THREAD @@ -1030,12 +1261,10 @@ void CASetNetworkMonitorCallback(CANetworkMonitorCallback nwMonitorHandler) g_nwMonitorHandler = nwMonitorHandler; } -CAResult_t CAInitializeMessageHandler() +CAResult_t CAInitializeMessageHandler(CATransportAdapter_t transportType) { - CASetPacketReceivedCallback(CAReceivedPacketCallback); - - CASetNetworkChangeCallback(CANetworkChangedCallback); - CASetErrorHandleCallback(CAErrorHandler); + CASetPacketReceivedCallback((CANetworkPacketReceivedCallback)CAReceivedPacketCallback); + CASetErrorHandleCallback((CAErrorHandleCallback)CAErrorHandler); #ifndef SINGLE_THREAD // create thread pool @@ -1052,19 +1281,18 @@ CAResult_t CAInitializeMessageHandler() if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread"); - ca_thread_pool_free(g_threadPoolHandle); - g_threadPoolHandle = NULL; return res; } // start send thread +#ifndef __TIZENRT__ res = CAQueueingThreadStart(&g_sendThread); +#else + res = CAQueueingThreadStart(&g_sendThread, "IoT_MessageHandlerQueue"); +#endif 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; } @@ -1074,9 +1302,6 @@ CAResult_t CAInitializeMessageHandler() if (CA_STATUS_OK != res) { OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread"); - ca_thread_pool_free(g_threadPoolHandle); - g_threadPoolHandle = NULL; - CAQueueingThreadDestroy(&g_sendThread); return res; } @@ -1086,10 +1311,6 @@ CAResult_t CAInitializeMessageHandler() 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 @@ -1100,10 +1321,6 @@ CAResult_t CAInitializeMessageHandler() 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; } @@ -1113,11 +1330,6 @@ CAResult_t CAInitializeMessageHandler() 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 @@ -1127,16 +1339,12 @@ CAResult_t CAInitializeMessageHandler() 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; } // initialize interface adapters by controller - CAInitializeAdapters(g_threadPoolHandle); + CAInitializeAdapters(g_threadPoolHandle, transportType); + CASetNetworkMonitorCallbacks(CAAdapterStateChangedCallback, CAConnectionStateChangedCallback); #else // retransmission initialize CAResult_t res = CARetransmissionInitialize(&g_retransmissionContext, NULL, CASendUnicastData, @@ -1153,6 +1361,49 @@ CAResult_t CAInitializeMessageHandler() return CA_STATUS_OK; } +static bool CAClearQueueAdapterDataContext(void *data, uint32_t size, void *ctx) +{ + (void)size; + + if (NULL == data || NULL == ctx) + { + return false; + } + + CAData_t *caData = (CAData_t *)data; + CATransportAdapter_t *type = (CATransportAdapter_t *)ctx; + + if (NULL != caData && NULL != caData->remoteEndpoint + && caData->remoteEndpoint->adapter == *type) + { + return true; + } + return false; +} + +void CAClearMessageHandler(CATransportAdapter_t transportType) +{ + CATransportAdapter_t *typeCtx = &transportType; + + CAResult_t res = CAQueueingThreadClearContextData(&g_sendThread, + CAClearQueueAdapterDataContext, + typeCtx); + + if (res != CA_STATUS_OK) + { + OIC_LOG_V(ERROR, TAG, "Clear send data failed[%d]", res); + } + + if (transportType & DEFAULT_RETRANSMISSION_TYPE) + { + res = CARetransmissionClearAdapterData(&g_retransmissionContext, transportType); + if (res != CA_STATUS_OK) + { + OIC_LOG_V(ERROR, TAG, "Clear retransmission data failed[%d]", res); + } + } +} + void CATerminateMessageHandler() { #ifndef SINGLE_THREAD @@ -1160,6 +1411,10 @@ void CATerminateMessageHandler() u_arraylist_t *list = CAGetSelectedNetworkList(); uint32_t length = u_arraylist_length(list); + #ifdef WITH_PROCESS_EVENT + g_processEvent = NULL; +#endif + uint32_t i = 0; for (i = 0; i < length; i++) { @@ -1222,36 +1477,9 @@ void CATerminateMessageHandler() #endif // SINGLE_THREAD } -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); - -#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->coap_hdr_udp_t.code); - - OIC_LOG(DEBUG, TAG, "PDU Maker - token :"); - - 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) { @@ -1288,11 +1516,16 @@ void CAErrorHandler(const CAEndpoint_t *endpoint, CAResult_t result) { OIC_LOG(DEBUG, TAG, "CAErrorHandler IN"); - -#ifndef SINGLE_THREAD VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint"); VERIFY_NON_NULL_VOID(data, TAG, "data"); + if (0 == dataLen) + { + OIC_LOG(ERROR, TAG, "dataLen is zero"); + return; + } + +#ifndef SINGLE_THREAD uint32_t code = CA_NOT_FOUND; //Do not free remoteEndpoint and data. Currently they will be freed in data thread //Get PDU data @@ -1304,7 +1537,7 @@ void CAErrorHandler(const CAEndpoint_t *endpoint, } 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); @@ -1313,8 +1546,24 @@ void CAErrorHandler(const CAEndpoint_t *endpoint, cadata->errorInfo->result = result; - CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t)); + CAResult_t res = CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t), false); + if (res != CA_STATUS_OK) + { + OIC_LOG(ERROR, TAG, "Failed to add message to data queue!"); + CADestroyData(cadata, sizeof(CAData_t)); + coap_delete_pdu(pdu); + return; + } + +#ifdef WITH_PROCESS_EVENT + if (g_processEvent) + { + oc_event_signal(g_processEvent); + } +#endif coap_delete_pdu(pdu); +#else + (void)result; #endif OIC_LOG(DEBUG, TAG, "CAErrorHandler OUT"); @@ -1364,7 +1613,340 @@ static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info, cadata->errorInfo = errorInfo; cadata->dataType = CA_ERROR_DATA; - CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t)); + res = CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t), false); + if (res != CA_STATUS_OK) + { + OIC_LOG(ERROR, TAG, "Failed to add message to data queue!"); + CADestroyData(cadata, sizeof(CAData_t)); + return; + } + +#ifdef WITH_PROCESS_EVENT + if (g_processEvent) + { + oc_event_signal(g_processEvent); + } +#endif//WITH_PROCESS_EVENT #endif OIC_LOG(DEBUG, TAG, "CASendErrorInfo OUT"); } + + + +#ifndef ARDUINO +#ifdef __TIZENRT__ +static void CALogPDUInfo(const CAData_t *data, const coap_pdu_t *pdu) +{ + + if(data == NULL || pdu == NULL) + { + printf("INVALID INPUT, CALogPDUInfo FAIL\n"); + } + + char type[30] = ""; + + switch(data->dataType) + { + case CA_REQUEST_DATA: + strncpy(type, "\e[32mREQUEST <<<<\e[m", 30); + break; + case CA_RESPONSE_DATA: + strncpy(type, "\e[36mRESPONSE >>>>\e[m", 30); + break; + case CA_ERROR_DATA: + strncpy(type, "ERROR", 30); + break; + case CA_RESPONSE_FOR_RES: + strncpy(type, "RESP_RES >>>>", 30); + break; + default: + snprintf(type, 30, "Type : %d", data->dataType); + break; + } + + + char method[20] = ""; + const CAInfo_t *info = NULL; + if (NULL != data->requestInfo) + { + switch(data->requestInfo->method) + { + case CA_GET: + strncpy(method, "GET", 20); + break; + case CA_POST: + strncpy(method, "POST", 20); + break; + case CA_PUT: + strncpy(method, "PUT", 20); + break; + case CA_DELETE: + strncpy(method, "DEL", 20); + break; + default: + sprintf(method, "Method : %d", data->requestInfo->method); + break; + } + info = &data->requestInfo->info; + } + + if(NULL != data->responseInfo) + { + + sprintf(method, "result : %d", data->responseInfo->result); + info = &data->responseInfo->info; + } + + + char log_buffer[1024] = ""; + sprintf(log_buffer, "CA_LOG [%5d] | %-13s | %-12s | msg size : %4d | %s", pdu->transport_hdr->udp.id , type, method, pdu->length, info->resourceUri); + + if(NULL != info) + { + sprintf(log_buffer, "CA_LOG [%5d] | %-13s | %-12s | msg size : %4d | %s", pdu->transport_hdr->udp.id , type, method, pdu->length, info->resourceUri); + } + + puts(log_buffer); +} + + +#else + +static void CALogPDUInfo(const CAData_t *data, const coap_pdu_t *pdu) +{ + OIC_LOG(DEBUG, TAG, "CALogPDUInfo"); + + VERIFY_NON_NULL_VOID(data, TAG, "data"); + VERIFY_NON_NULL_VOID(pdu, TAG, "pdu"); + OIC_TRACE_BEGIN(%s:CALogPDUInfo, TAG); + + OIC_LOG(INFO, ANALYZER_TAG, "================================================="); + if(SEND_TYPE_MULTICAST == data->type) + { + OIC_LOG(INFO, ANALYZER_TAG, "Is Multicast = true"); + } + else + { + OIC_LOG(INFO, ANALYZER_TAG, "Is Multicast = false"); + } + + if (NULL != data->remoteEndpoint) + { + CALogAdapterTypeInfo(data->remoteEndpoint->adapter); + OIC_LOG_V(DEBUG, ANALYZER_TAG, "Address = [%s]:[%d]", data->remoteEndpoint->addr, + data->remoteEndpoint->port); + } + + switch(data->dataType) + { + case CA_REQUEST_DATA: + OIC_LOG(INFO, ANALYZER_TAG, "Data Type = [CA_REQUEST_DATA]"); + break; + case CA_RESPONSE_DATA: + OIC_LOG(INFO, ANALYZER_TAG, "Data Type = [CA_RESPONSE_DATA]"); + break; + case CA_ERROR_DATA: + OIC_LOG(INFO, ANALYZER_TAG, "Data Type = [CA_ERROR_DATA]"); + break; + case CA_RESPONSE_FOR_RES: + OIC_LOG(INFO, ANALYZER_TAG, "Data Type = [CA_RESPONSE_FOR_RES]"); + break; + default: + OIC_LOG_V(INFO, ANALYZER_TAG, "Data Type = [%d]", data->dataType); + break; + } + + const CAInfo_t *info = NULL; + if (NULL != data->requestInfo) + { + switch(data->requestInfo->method) + { + case CA_GET: + OIC_LOG(INFO, ANALYZER_TAG, "Method = [GET]"); + break; + case CA_POST: + OIC_LOG(INFO, ANALYZER_TAG, "Method = [POST]"); + break; + case CA_PUT: + OIC_LOG(INFO, ANALYZER_TAG, "Method = [PUT]"); + break; + case CA_DELETE: + OIC_LOG(INFO, ANALYZER_TAG, "Method = [DELETE]"); + break; + default: + OIC_LOG_V(INFO, ANALYZER_TAG, "Method = [%d]", data->requestInfo->method); + break; + } + info = &data->requestInfo->info; + } + + if (NULL != data->responseInfo) + { + OIC_LOG_V(INFO, ANALYZER_TAG, "result code = [%d]", data->responseInfo->result); + info = &data->responseInfo->info; + } + + if (pdu->transport_hdr) + { + OIC_LOG_V(INFO, ANALYZER_TAG, "Msg ID = [%d]", pdu->transport_hdr->udp.id); + } + + if (info) + { + OIC_LOG(INFO, ANALYZER_TAG, "Coap Token"); + OIC_LOG_BUFFER(INFO, ANALYZER_TAG, (const uint8_t *) info->token, info->tokenLength); + OIC_TRACE_BUFFER("OIC_CA_MSG_HANDLE:CALogPDUInfo:token", + (const uint8_t *) info->token, info->tokenLength); + OIC_LOG_V(INFO_PRIVATE, ANALYZER_TAG, "Res URI = [%s]", info->resourceUri); + OIC_TRACE_MARK(%s:CALogPDUInfo:uri:%s, TAG, info->resourceUri); + + if (CA_FORMAT_APPLICATION_CBOR == info->payloadFormat) + { + OIC_LOG(INFO, ANALYZER_TAG, "Payload Format = [CA_FORMAT_APPLICATION_CBOR]"); + } + else + { + OIC_LOG_V(INFO, ANALYZER_TAG, "Payload Format = [%d]", info->payloadFormat); + } + } + + size_t payloadLen = (pdu->data) ? (unsigned char *) pdu->hdr + pdu->length - pdu->data : 0; + OIC_LOG_V(INFO, ANALYZER_TAG, "CoAP Message Full Size = [%u]", pdu->length); + OIC_LOG(INFO, ANALYZER_TAG, "CoAP Header (+ 0xFF)"); + OIC_LOG_BUFFER(INFO, ANALYZER_TAG, (const uint8_t *) pdu->transport_hdr, + pdu->length - payloadLen); + OIC_LOG_V(INFO, ANALYZER_TAG, "CoAP Header size = [%" PRIuPTR "]", (size_t) pdu->length - payloadLen); + + OIC_LOG_V(INFO, ANALYZER_TAG, "CoAP Payload"); + OIC_LOG_BUFFER(INFO_PRIVATE, ANALYZER_TAG, pdu->data, payloadLen); + OIC_LOG_V(INFO, ANALYZER_TAG, "CoAP Payload Size = [%" PRIuPTR "]", payloadLen); + OIC_LOG(INFO, ANALYZER_TAG, "================================================="); + + // samsung log + CASamsungLogMessage(data, pdu); + OIC_TRACE_END(); +} + +static void CASamsungLogMessage(const CAData_t *data, const coap_pdu_t *pdu) +{ + OIC_LOG(INFO, TAG, "CASamsungLogMessage"); + VERIFY_NON_NULL_VOID(data, TAG, "data"); + VERIFY_NON_NULL_VOID(pdu, TAG, "pdu"); + VERIFY_NON_NULL_VOID(data->remoteEndpoint, TAG, "data->remoteEndpoint"); + + const CAInfo_t *info = NULL; + if (NULL != data->requestInfo) + { + info = &data->requestInfo->info; + } + + if (NULL != data->responseInfo) + { + info = &data->responseInfo->info; + } + + VERIFY_NON_NULL_VOID(info, TAG, "info"); + + memset(g_headerBuffer, 0, MAX_LOG_BUFFER_SIZE); + g_headerIndex = 0; + + g_headerBuffer[g_headerIndex++] = data->dataType; + g_headerBuffer[g_headerIndex++] = '|'; + g_headerBuffer[g_headerIndex++] = data->remoteEndpoint->adapter; + g_headerBuffer[g_headerIndex++] = '|'; + g_headerBuffer[g_headerIndex++] = data->type; + g_headerBuffer[g_headerIndex++] = '|'; + + if (NULL != data->remoteEndpoint) + { + int i = 0; + while (data->remoteEndpoint->addr[i]) + { + g_headerBuffer[g_headerIndex++] = data->remoteEndpoint->addr[i]; + i++; + } + g_headerBuffer[g_headerIndex++] = ':'; + g_headerBuffer[g_headerIndex++] = (data->remoteEndpoint->port >> 8) & 0x0000ff; + g_headerBuffer[g_headerIndex++] = data->remoteEndpoint->port & 0x000000ff; + } + + g_headerBuffer[g_headerIndex++] = '|'; + if (data->requestInfo) + { + g_headerBuffer[g_headerIndex++] = data->requestInfo->method; + } + else + { + g_headerBuffer[g_headerIndex++] = 0; + } + + g_headerBuffer[g_headerIndex++] = '|'; + if (data->responseInfo) + { + g_headerBuffer[g_headerIndex++] = data->responseInfo->result; + } + else + { + g_headerBuffer[g_headerIndex++] = 0; + } + g_headerBuffer[g_headerIndex++] = '|'; + + if (pdu->transport_hdr) + { + g_headerBuffer[g_headerIndex++] = (pdu->transport_hdr->udp.id >> 8) & 0x0000ff; + g_headerBuffer[g_headerIndex++] = pdu->transport_hdr->udp.id & 0x000000ff; + } + else + { + g_headerBuffer[g_headerIndex++] = 0; + g_headerBuffer[g_headerIndex++] = 0; + } + g_headerBuffer[g_headerIndex++] = '|'; + + if (info->token && info->tokenLength > 0) + { + for (size_t i = 0; i < info->tokenLength; i++) + { + g_headerBuffer[g_headerIndex++] = info->token[i]; + } + g_headerBuffer[g_headerIndex++] = '|'; + } + + if (info->resourceUri) + { + size_t i = 0; + while (info->resourceUri[i]) + { + g_headerBuffer[g_headerIndex++] = info->resourceUri[i]; + i++; + } + g_headerBuffer[g_headerIndex++] = '|'; + } + + OIC_LOG_CA_BUFFER(INFO, TAG, (uint8_t *) g_headerBuffer, g_headerIndex, 1); + size_t payloadLen = (unsigned char *) pdu->hdr + pdu->length - pdu->data; + OIC_LOG_CA_BUFFER(INFO_PRIVATE, TAG, pdu->data, payloadLen, 0); +} +#endif + +#else +static void CALogPDUInfo(const CAData_t *data, const coap_pdu_t *pdu) +{ + VERIFY_NON_NULL_VOID(pdu, TAG, "pdu"); + (void)data; + + OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data); + OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->transport_hdr->udp.type); + OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->transport_hdr->udp.code); + OIC_LOG(DEBUG, TAG, "PDU Maker - token :"); + OIC_LOG_BUFFER(DEBUG, TAG, pdu->transport_hdr->udp.token, + pdu->transport_hdr->udp.token_length); +} +#endif + +#ifdef WITH_PROCESS_EVENT +void CARegisterMessageProcessEvent(oc_event event) +{ + g_processEvent = event; +} +#endif // WITH_PROCESS_EVENT