1 /* *****************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
26 #include "cainterface.h"
27 #include "camessagehandler.h"
28 #include "caremotehandler.h"
29 #include "caprotocolmessage.h"
32 #ifndef WITH_UPSTREAM_LIBCOAP
33 #include "coap/config.h"
35 #include "oic_malloc.h"
36 #include "canetworkconfigurator.h"
37 #include "caadapterutils.h"
38 #include "cainterfacecontroller.h"
39 #include "caretransmission.h"
40 #include "oic_string.h"
43 #include "cablockwisetransfer.h"
48 #include "cathreadpool.h" /* for thread pool */
49 #include "caqueueingthread.h"
52 #define MAX_THREAD_POOL_SIZE 20
54 #define UNUSED(x) (void)(x)
57 static ca_thread_pool_t g_threadPoolHandle = NULL;
59 // message handler main thread
60 static CAQueueingThread_t g_sendThread;
61 static CAQueueingThread_t g_receiveThread;
64 #define CA_MAX_RT_ARRAY_SIZE 3
65 #endif // SINGLE_THREAD
67 #define TAG "OIC_CA_MSG_HANDLE"
69 static CARetransmission_t g_retransmissionContext;
72 static CARequestCallback g_requestHandler = NULL;
73 static CAResponseCallback g_responseHandler = NULL;
74 static CAErrorCallback g_errorHandler = NULL;
75 static CANetworkMonitorCallback g_nwMonitorHandler = NULL;
77 static void CAErrorHandler(const CAEndpoint_t *endpoint,
78 const void *data, uint32_t dataLen,
81 static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint,
82 const CARemoteId_t *identity,
83 const void *data, CADataType_t dataType);
85 static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info,
89 static void CAProcessReceivedData(CAData_t *data);
91 static void CADestroyData(void *data, uint32_t size);
92 static void CALogPayloadInfo(CAInfo_t *info);
93 static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *endpoint, uint16_t id,
94 CAToken_t token, uint8_t tokenLength);
97 * print send / receive message of CoAP.
98 * @param[in] data CA information which has send/receive message and endpoint.
99 * @param[in] pdu CoAP pdu low data.
101 static void CALogPDUInfo(const CAData_t *data, const coap_pdu_t *pdu);
104 static char g_headerBuffer[MAX_LOG_BUFFER_SIZE] = {0};
105 static size_t g_headerIndex = 0;
106 static void CASamsungLogMessage(const CAData_t *data, const coap_pdu_t *pdu);
110 void CAAddDataToSendThread(CAData_t *data)
112 VERIFY_NON_NULL_VOID(data, TAG, "data");
115 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
118 void CAAddDataToReceiveThread(CAData_t *data)
120 VERIFY_NON_NULL_VOID(data, TAG, "data");
123 CAQueueingThreadAddData(&g_receiveThread, data, sizeof(CAData_t));
127 static bool CAIsSelectedNetworkAvailable()
129 u_arraylist_t *list = CAGetSelectedNetworkList();
130 if (!list || u_arraylist_length(list) == 0)
132 OIC_LOG(ERROR, TAG, "No selected network");
139 static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint,
140 const CARemoteId_t *identity,
141 const void *data, CADataType_t dataType)
143 OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData IN");
144 CAInfo_t *info = NULL;
145 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
148 OIC_LOG(ERROR, TAG, "memory allocation failed");
152 CAEndpoint_t* ep = endpoint;
154 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
157 OIC_LOG(ERROR, TAG, "endpoint clone failed");
162 OIC_LOG_V(DEBUG, TAG, "address : %s", ep->addr);
164 if (CA_RESPONSE_DATA == dataType)
166 CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
169 OIC_LOG(ERROR, TAG, "memory allocation failed");
173 CAResult_t result = CAGetResponseInfoFromPDU(data, resInfo, endpoint);
174 if (CA_STATUS_OK != result)
176 OIC_LOG(ERROR, TAG, "CAGetResponseInfoFromPDU Failed");
177 CADestroyResponseInfoInternal(resInfo);
180 cadata->responseInfo = resInfo;
181 info = &resInfo->info;
184 info->identity = *identity;
186 OIC_LOG(DEBUG, TAG, "Response Info :");
187 CALogPayloadInfo(info);
189 else if (CA_REQUEST_DATA == dataType)
191 CARequestInfo_t* reqInfo = (CARequestInfo_t*)OICCalloc(1, sizeof(CARequestInfo_t));
194 OIC_LOG(ERROR, TAG, "memory allocation failed");
198 CAResult_t result = CAGetRequestInfoFromPDU(data, endpoint, reqInfo);
199 if (CA_STATUS_OK != result)
201 OIC_LOG(ERROR, TAG, "CAGetRequestInfoFromPDU failed");
202 CADestroyRequestInfoInternal(reqInfo);
206 if ((reqInfo->info.type != CA_MSG_CONFIRM) &&
207 CADropSecondMessage(&caglobals.ca.requestHistory, endpoint, reqInfo->info.messageId,
208 reqInfo->info.token, reqInfo->info.tokenLength))
210 OIC_LOG(INFO, TAG, "Second Request with same Token, Drop it");
211 CADestroyRequestInfoInternal(reqInfo);
215 cadata->requestInfo = reqInfo;
216 info = &reqInfo->info;
219 info->identity = *identity;
221 OIC_LOG(DEBUG, TAG, "Request Info :");
222 CALogPayloadInfo(info);
224 else if (CA_ERROR_DATA == dataType)
226 CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
229 OIC_LOG(ERROR, TAG, "Memory allocation failed!");
233 CAResult_t result = CAGetErrorInfoFromPDU(data, endpoint, errorInfo);
234 if (CA_STATUS_OK != result)
236 OIC_LOG(ERROR, TAG, "CAGetErrorInfoFromPDU failed");
241 cadata->errorInfo = errorInfo;
242 info = &errorInfo->info;
245 info->identity = *identity;
247 OIC_LOG(DEBUG, TAG, "error Info :");
248 CALogPayloadInfo(info);
251 cadata->remoteEndpoint = ep;
252 cadata->dataType = dataType;
254 OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData OUT");
259 #ifndef SINGLE_THREAD
265 static void CATimeoutCallback(const CAEndpoint_t *endpoint, const void *pdu, uint32_t size)
267 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
268 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
270 CAEndpoint_t* ep = endpoint;
272 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
275 OIC_LOG(ERROR, TAG, "clone failed");
280 CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
284 OIC_LOG(ERROR, TAG, "calloc failed");
285 #ifndef SINGLE_THREAD
291 resInfo->result = CA_RETRANSMIT_TIMEOUT;
292 resInfo->info.type = CAGetMessageTypeFromPduBinaryData(pdu, size);
293 resInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
295 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_transport_t *) pdu, &(resInfo->info),
297 if (CA_STATUS_OK != res)
299 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
300 CADestroyResponseInfoInternal(resInfo);
301 #ifndef SINGLE_THREAD
307 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
310 OIC_LOG(ERROR, TAG, "memory allocation failed !");
311 #ifndef SINGLE_THREAD
314 CADestroyResponseInfoInternal(resInfo);
318 cadata->type = SEND_TYPE_UNICAST;
319 cadata->remoteEndpoint = ep;
320 cadata->requestInfo = NULL;
321 cadata->responseInfo = resInfo;
324 if (CAIsSupportedBlockwiseTransfer(endpoint->adapter))
326 res = CARemoveBlockDataFromListWithSeed(resInfo->info.token, resInfo->info.tokenLength,
328 if (CA_STATUS_OK != res)
330 OIC_LOG(ERROR, TAG, "CARemoveBlockDataFromListWithSeed failed");
336 CAProcessReceivedData(cadata);
338 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
342 static void CADestroyData(void *data, uint32_t size)
344 OIC_LOG(DEBUG, TAG, "CADestroyData IN");
345 if ((size_t)size < sizeof(CAData_t))
347 OIC_LOG_V(ERROR, TAG, "Destroy data too small %p %d", data, size);
349 CAData_t *cadata = (CAData_t *) data;
353 OIC_LOG(ERROR, TAG, "cadata is NULL");
356 #ifndef SINGLE_THREAD
357 if (NULL != cadata->remoteEndpoint)
359 CAFreeEndpoint(cadata->remoteEndpoint);
363 if (NULL != cadata->requestInfo)
365 CADestroyRequestInfoInternal((CARequestInfo_t *) cadata->requestInfo);
368 if (NULL != cadata->responseInfo)
370 CADestroyResponseInfoInternal((CAResponseInfo_t *) cadata->responseInfo);
373 if (NULL != cadata->errorInfo)
375 CADestroyErrorInfoInternal(cadata->errorInfo);
379 OIC_LOG(DEBUG, TAG, "CADestroyData OUT");
383 static void CAProcessReceivedData(CAData_t *data)
385 OIC_LOG(DEBUG, TAG, "CAProcessReceivedData IN");
388 OIC_LOG(ERROR, TAG, "thread data error!!");
392 // parse the data and call the callbacks.
395 CAEndpoint_t *rep = (CAEndpoint_t *)(data->remoteEndpoint);
398 OIC_LOG(ERROR, TAG, "remoteEndpoint error!!");
402 if (data->requestInfo && g_requestHandler)
404 g_requestHandler(rep, data->requestInfo);
406 else if (data->responseInfo && g_responseHandler)
408 g_responseHandler(rep, data->responseInfo);
410 else if (data->errorInfo && g_errorHandler)
412 g_errorHandler(rep, data->errorInfo);
415 CADestroyData(data, sizeof(CAData_t));
417 OIC_LOG(DEBUG, TAG, "CAProcessReceivedData OUT");
421 #ifndef SINGLE_THREAD
422 static void CAReceiveThreadProcess(void *threadData)
424 #ifndef SINGLE_HANDLE
425 CAData_t *data = (CAData_t *) threadData;
426 OIC_TRACE_BEGIN(%s:CAProcessReceivedData, TAG);
427 CAProcessReceivedData(data);
433 #endif // SINGLE_THREAD
435 static CAResult_t CAProcessMulticastData(const CAData_t *data)
437 VERIFY_NON_NULL(data, TAG, "data");
438 VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");
440 coap_pdu_t *pdu = NULL;
441 CAInfo_t *info = NULL;
442 coap_list_t *options = NULL;
443 coap_transport_t transport = COAP_UDP;
444 CAResult_t res = CA_SEND_FAILED;
446 if (!data->requestInfo && !data->responseInfo)
448 OIC_LOG(ERROR, TAG, "request or response info is empty");
452 if (data->requestInfo)
454 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
456 info = &data->requestInfo->info;
457 pdu = CAGeneratePDU(CA_GET, info, data->remoteEndpoint, &options, &transport);
459 else if (data->responseInfo)
461 OIC_LOG(DEBUG, TAG, "responseInfo is available..");
463 info = &data->responseInfo->info;
464 pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint,
465 &options, &transport);
470 OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
471 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
472 coap_delete_list(options);
477 if (CAIsSupportedBlockwiseTransfer(data->remoteEndpoint->adapter))
479 // Blockwise transfer
480 res = CAAddBlockOption(&pdu, info, data->remoteEndpoint, &options);
481 if (CA_STATUS_OK != res)
483 OIC_LOG(DEBUG, TAG, "CAAddBlockOption has failed");
489 CALogPDUInfo(data, pdu);
491 res = CASendMulticastData(data->remoteEndpoint, pdu->transport_hdr, pdu->length, data->dataType);
492 if (CA_STATUS_OK != res)
494 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
498 coap_delete_list(options);
499 coap_delete_pdu(pdu);
503 CAErrorHandler(data->remoteEndpoint, pdu->transport_hdr, pdu->length, res);
504 coap_delete_list(options);
505 coap_delete_pdu(pdu);
509 static CAResult_t CAProcessSendData(const CAData_t *data)
511 VERIFY_NON_NULL(data, TAG, "data");
512 VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");
514 CAResult_t res = CA_STATUS_FAILED;
516 if (CA_NETWORK_COMMAND == data->dataType)
518 if (CA_REQ_DISCONNECT == data->eventInfo)
521 // request TCP disconnect
522 if (CA_ADAPTER_TCP == data->remoteEndpoint->adapter)
524 OIC_LOG(INFO, TAG, "request TCP disconnect");
525 return CADisconnectSession(data->remoteEndpoint);
531 CASendDataType_t type = data->type;
533 coap_pdu_t *pdu = NULL;
534 CAInfo_t *info = NULL;
535 coap_list_t *options = NULL;
536 coap_transport_t transport = COAP_UDP;
538 if (SEND_TYPE_UNICAST == type)
540 OIC_LOG(DEBUG,TAG,"Unicast message");
542 #ifdef ROUTING_GATEWAY
544 * When forwarding a packet, do not attempt retransmission as its the responsibility of
545 * packet originator node
547 bool skipRetransmission = false;
550 if (NULL != data->requestInfo)
552 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
554 info = &data->requestInfo->info;
555 #ifdef ROUTING_GATEWAY
556 skipRetransmission = data->requestInfo->info.skipRetransmission;
558 pdu = CAGeneratePDU(data->requestInfo->method, info, data->remoteEndpoint,
559 &options, &transport);
561 else if (NULL != data->responseInfo)
563 OIC_LOG(DEBUG, TAG, "responseInfo is available..");
565 info = &data->responseInfo->info;
566 #ifdef ROUTING_GATEWAY
567 skipRetransmission = data->responseInfo->info.skipRetransmission;
569 pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint,
570 &options, &transport);
574 OIC_LOG(DEBUG, TAG, "request info, response info is empty");
575 return CA_STATUS_INVALID_PARAM;
578 // interface controller function call.
582 if (CAIsSupportedBlockwiseTransfer(data->remoteEndpoint->adapter))
584 // Blockwise transfer
587 CAResult_t res = CAAddBlockOption(&pdu, info,
588 data->remoteEndpoint,
590 if (CA_STATUS_OK != res)
592 OIC_LOG(INFO, TAG, "to write block option has failed");
593 CAErrorHandler(data->remoteEndpoint, pdu->transport_hdr, pdu->length, res);
594 coap_delete_list(options);
595 coap_delete_pdu(pdu);
601 CALogPDUInfo(data, pdu);
603 OIC_LOG_V(INFO, TAG, "CASendUnicastData type : %d", data->dataType);
604 res = CASendUnicastData(data->remoteEndpoint, pdu->transport_hdr, pdu->length, data->dataType);
605 if (CA_STATUS_OK != res)
607 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
608 CAErrorHandler(data->remoteEndpoint, pdu->transport_hdr, pdu->length, res);
609 coap_delete_list(options);
610 coap_delete_pdu(pdu);
615 if (CAIsSupportedCoAPOverTCP(data->remoteEndpoint->adapter))
617 OIC_LOG(INFO, TAG, "retransmission will be not worked");
621 #ifdef ROUTING_GATEWAY
622 if (!skipRetransmission)
625 // for retransmission
626 res = CARetransmissionSentData(&g_retransmissionContext,
627 data->remoteEndpoint,
629 pdu->transport_hdr, pdu->length);
630 if ((CA_STATUS_OK != res) && (CA_NOT_SUPPORTED != res))
632 //when retransmission not supported this will return CA_NOT_SUPPORTED, ignore
633 OIC_LOG_V(INFO, TAG, "retransmission is not enabled due to error, res : %d", res);
634 coap_delete_list(options);
635 coap_delete_pdu(pdu);
640 coap_delete_list(options);
641 coap_delete_pdu(pdu);
645 OIC_LOG(ERROR,TAG,"Failed to generate unicast PDU");
646 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
647 return CA_SEND_FAILED;
650 else if (SEND_TYPE_MULTICAST == type)
652 OIC_LOG(DEBUG,TAG,"Multicast message");
655 * If CoAP over TCP is enabled, the CoAP pdu wont be same for IP and other adapters.
656 * That's why we need to generate two pdu's, one for IP and second for other transports.
657 * Two possible cases we might have to split: a) when adapter is CA_DEFAULT_ADAPTER
658 * b) when one of the adapter is IP adapter(ex: CA_ADAPTER_IP | CA_ADAPTER_GATT_BTLE)
660 if (data->remoteEndpoint->adapter == CA_DEFAULT_ADAPTER ||
661 (CA_ADAPTER_IP & data->remoteEndpoint->adapter &&
662 CA_ADAPTER_IP != data->remoteEndpoint->adapter))
664 if (data->remoteEndpoint->adapter == CA_DEFAULT_ADAPTER)
666 data->remoteEndpoint->adapter = CA_ALL_ADAPTERS ^ CA_ADAPTER_IP;
670 data->remoteEndpoint->adapter = data->remoteEndpoint->adapter ^ CA_ADAPTER_IP;
672 CAProcessMulticastData(data);
673 data->remoteEndpoint->adapter = CA_ADAPTER_IP;
674 CAProcessMulticastData(data);
678 CAProcessMulticastData(data);
681 CAProcessMulticastData(data);
687 #ifndef SINGLE_THREAD
688 static void CASendThreadProcess(void *threadData)
690 CAData_t *data = (CAData_t *) threadData;
691 OIC_TRACE_BEGIN(%s:CAProcessSendData, TAG);
692 CAProcessSendData(data);
698 * If a second message arrives with the same message ID, token and the other address
699 * family, drop it. Typically, IPv6 beats IPv4, so the IPv4 message is dropped.
701 static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *ep, uint16_t id,
702 CAToken_t token, uint8_t tokenLength)
708 if (ep->adapter != CA_ADAPTER_IP)
712 if (!caglobals.ip.dualstack)
717 if (tokenLength > CA_MAX_TOKEN_LEN)
720 * If token length is more than CA_MAX_TOKEN_LEN,
721 * we compare the first CA_MAX_TOKEN_LEN bytes only.
723 tokenLength = CA_MAX_TOKEN_LEN;
727 CATransportFlags_t familyFlags = ep->flags & CA_IPFAMILY_MASK;
729 for (size_t i = 0; i < sizeof(history->items) / sizeof(history->items[0]); i++)
731 CAHistoryItem_t *item = &(history->items[i]);
732 if (id == item->messageId && tokenLength == item->tokenLength
733 && memcmp(item->token, token, tokenLength) == 0)
735 if ((familyFlags ^ item->flags) == CA_IPFAMILY_MASK)
737 OIC_LOG_V(INFO, TAG, "IPv%c duplicate message ignored",
738 familyFlags & CA_IPV6 ? '6' : '4');
745 history->items[history->nextIndex].flags = familyFlags;
746 history->items[history->nextIndex].messageId = id;
747 if (token && tokenLength)
749 memcpy(history->items[history->nextIndex].token, token, tokenLength);
750 history->items[history->nextIndex].tokenLength = tokenLength;
753 if (++history->nextIndex >= HISTORYSIZE)
755 history->nextIndex = 0;
761 static CAResult_t CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
762 const void *data, uint32_t dataLen)
764 VERIFY_NON_NULL(sep, TAG, "remoteEndpoint");
765 VERIFY_NON_NULL(data, TAG, "data");
766 OIC_TRACE_BEGIN(%s:CAReceivedPacketCallback, TAG);
770 OIC_LOG(ERROR, TAG, "dataLen is zero");
773 return CA_STATUS_FAILED;
777 OIC_LOG(DEBUG, TAG, "received pdu data :");
780 OIC_LOG_BUFFER(DEBUG, TAG, data, dataLen);
784 OIC_LOG_BUFFER(DEBUG, TAG, data, 32);
787 CAResult_t res = CA_STATUS_OK;
788 uint32_t code = CA_NOT_FOUND;
789 CAData_t *cadata = NULL;
791 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code,
795 OIC_LOG(ERROR, TAG, "Parse PDU failed");
796 res = CA_STATUS_FAILED;
800 OIC_LOG_V(DEBUG, TAG, "code = %d", code);
801 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
803 cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_REQUEST_DATA);
806 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
807 coap_delete_pdu(pdu);
813 cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_RESPONSE_DATA);
816 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
817 coap_delete_pdu(pdu);
822 if (CAIsSupportedCoAPOverTCP(sep->endpoint.adapter))
824 OIC_LOG(INFO, TAG, "retransmission is not supported");
829 // for retransmission
830 void *retransmissionPdu = NULL;
831 CARetransmissionReceivedData(&g_retransmissionContext, cadata->remoteEndpoint, pdu->transport_hdr,
832 pdu->length, &retransmissionPdu);
834 // get token from saved data in retransmission list
835 if (retransmissionPdu && CA_EMPTY == code)
837 if (cadata->responseInfo)
839 CAInfo_t *info = &cadata->responseInfo->info;
840 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_transport_t *)retransmissionPdu,
841 info, &(sep->endpoint));
842 if (CA_STATUS_OK != res)
844 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
845 OICFree(info->token);
846 info->tokenLength = 0;
850 OICFree(retransmissionPdu);
854 cadata->type = SEND_TYPE_UNICAST;
856 CALogPDUInfo(cadata, pdu);
859 CAProcessReceivedData(cadata);
862 if (CAIsSupportedBlockwiseTransfer(sep->endpoint.adapter))
864 CAResult_t res = CAReceiveBlockWiseData(pdu, &(sep->endpoint), cadata, dataLen);
865 if (CA_NOT_SUPPORTED == res || CA_REQUEST_TIMEOUT == res)
867 OIC_LOG(DEBUG, TAG, "this message does not have block option");
868 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
872 CADestroyData(cadata, sizeof(CAData_t));
878 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
880 #endif // SINGLE_THREAD
882 coap_delete_pdu(pdu);
885 OIC_LOG(DEBUG, TAG, "OUT - Recv Thread");
890 static void CAAdapterStateChangedCallback(CATransportAdapter_t transportType, bool enabled)
894 CAClearMessageHandler(transportType);
898 static bool CAClearQueueEndpointDataContext(void *data, uint32_t size, void *ctx)
901 if (NULL == data || NULL == ctx)
906 CAData_t *caData = (CAData_t *)data;
907 const CAEndpoint_t *endpoint = (const CAEndpoint_t *)ctx;
909 if (NULL != caData && NULL != caData->remoteEndpoint)
911 if (strcmp(caData->remoteEndpoint->addr, endpoint->addr) == 0
912 && caData->remoteEndpoint->port == endpoint->port
913 && caData->remoteEndpoint->adapter == endpoint->adapter)
921 static void CAConnectionStateChangedCallback(const CAEndpoint_t *info, bool isConnected)
925 CAResult_t res = CAQueueingThreadClearContextData(&g_sendThread,
926 CAClearQueueEndpointDataContext,
928 if (CA_STATUS_OK != res)
930 OIC_LOG(ERROR, TAG, "Could not clear the send queue");
935 void CAHandleRequestResponseCallbacks()
939 CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
942 // parse the data and call the callbacks.
946 oc_mutex_lock(g_receiveThread.threadMutex);
948 u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
950 oc_mutex_unlock(g_receiveThread.threadMutex);
952 if (NULL == item || NULL == item->msg)
958 CAData_t *td = (CAData_t *) item->msg;
960 if (td->requestInfo && g_requestHandler)
962 OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
963 g_requestHandler(td->remoteEndpoint, td->requestInfo);
965 else if (td->responseInfo && g_responseHandler)
967 OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
968 g_responseHandler(td->remoteEndpoint, td->responseInfo);
970 else if (td->errorInfo && g_errorHandler)
972 OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
973 g_errorHandler(td->remoteEndpoint, td->errorInfo);
976 CADestroyData(item->msg, sizeof(CAData_t));
979 #endif // SINGLE_HANDLE
980 #endif // SINGLE_THREAD
983 static CAData_t* CAPrepareSendData(const CAEndpoint_t *endpoint, const void *sendData,
984 CADataType_t dataType)
986 OIC_LOG(DEBUG, TAG, "CAPrepareSendData IN");
988 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
991 OIC_LOG(ERROR, TAG, "memory allocation failed");
995 if (CA_REQUEST_DATA == dataType)
998 CARequestInfo_t *request = (CARequestInfo_t *)sendData;
1000 // clone request info
1001 CARequestInfo_t *request = CACloneRequestInfo((CARequestInfo_t *)sendData);
1004 OIC_LOG(ERROR, TAG, "CACloneRequestInfo failed");
1008 cadata->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
1009 cadata->requestInfo = request;
1011 else if (CA_RESPONSE_DATA == dataType || CA_RESPONSE_FOR_RES == dataType)
1013 #ifdef SINGLE_THREAD
1014 CAResponseInfo_t *response = (CAResponseInfo_t *)sendData;
1016 // clone response info
1017 CAResponseInfo_t *response = CACloneResponseInfo((CAResponseInfo_t *)sendData);
1020 OIC_LOG(ERROR, TAG, "CACloneResponseInfo failed");
1024 cadata->type = response->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
1025 cadata->responseInfo = response;
1029 OIC_LOG(ERROR, TAG, "CAPrepareSendData unknown data type");
1033 #ifdef SINGLE_THREAD
1034 CAEndpoint_t* ep = endpoint;
1036 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
1039 OIC_LOG(ERROR, TAG, "endpoint clone failed");
1043 cadata->remoteEndpoint = ep;
1044 cadata->dataType = dataType;
1048 #ifndef SINGLE_THREAD
1049 CADestroyData(cadata, sizeof(CAData_t));
1056 CAResult_t CADetachSendNetworkReqMessage(const CAEndpoint_t *endpoint,
1057 CAConnectEvent_t event,
1058 CADataType_t dataType)
1060 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
1062 if (false == CAIsSelectedNetworkAvailable())
1064 return CA_STATUS_FAILED;
1067 #ifndef SINGLE_THREAD
1068 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
1071 OIC_LOG(ERROR, TAG, "cadata memory allocation failed");
1072 return CA_STATUS_FAILED;
1075 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
1078 OIC_LOG(ERROR, TAG, "endpoint clone failed");
1080 return CA_STATUS_FAILED;
1083 cadata->remoteEndpoint = ep;
1084 cadata->eventInfo = event;
1085 cadata->dataType = dataType;
1087 CAQueueingThreadAddData(&g_sendThread, cadata, sizeof(CAData_t));
1090 return CA_STATUS_OK;
1093 CAResult_t CADetachSendMessage(const CAEndpoint_t *endpoint, const void *sendMsg,
1094 CADataType_t dataType)
1096 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
1097 VERIFY_NON_NULL(sendMsg, TAG, "sendMsg");
1099 if (false == CAIsSelectedNetworkAvailable())
1101 return CA_STATUS_FAILED;
1105 // If max retransmission queue is reached, then don't handle new request
1106 if (CA_MAX_RT_ARRAY_SIZE == u_arraylist_length(g_retransmissionContext.dataList))
1108 OIC_LOG(ERROR, TAG, "max RT queue size reached!");
1109 return CA_SEND_FAILED;
1113 CAData_t *data = CAPrepareSendData(endpoint, sendMsg, dataType);
1116 OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
1117 return CA_MEMORY_ALLOC_FAILED;
1120 OIC_LOG_V(INFO_PRIVATE, TAG, "DID of endpoint of this message is %s", endpoint->remoteId);
1122 #ifdef SINGLE_THREAD
1123 CAResult_t result = CAProcessSendData(data);
1124 if (CA_STATUS_OK != result)
1126 OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
1135 if (CAIsSupportedBlockwiseTransfer(endpoint->adapter))
1137 CACheckAndDeleteTimedOutBlockData();
1139 CAResult_t res = CASendBlockWiseData(data);
1140 if (CA_NOT_SUPPORTED == res)
1142 OIC_LOG(DEBUG, TAG, "normal msg will be sent");
1143 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
1144 return CA_STATUS_OK;
1148 CADestroyData(data, sizeof(CAData_t));
1156 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
1158 #endif // SINGLE_THREAD
1160 return CA_STATUS_OK;
1163 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
1164 CAErrorCallback errorHandler)
1166 g_requestHandler = ReqHandler;
1167 g_responseHandler = RespHandler;
1168 g_errorHandler = errorHandler;
1171 void CASetNetworkMonitorCallback(CANetworkMonitorCallback nwMonitorHandler)
1173 g_nwMonitorHandler = nwMonitorHandler;
1176 CAResult_t CAInitializeMessageHandler(CATransportAdapter_t transportType)
1178 CASetPacketReceivedCallback((CANetworkPacketReceivedCallback)CAReceivedPacketCallback);
1179 CASetErrorHandleCallback((CAErrorHandleCallback)CAErrorHandler);
1181 #ifndef SINGLE_THREAD
1182 // create thread pool
1183 CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
1184 if (CA_STATUS_OK != res)
1186 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
1190 // send thread initialize
1191 res = CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
1192 CASendThreadProcess, CADestroyData);
1193 if (CA_STATUS_OK != res)
1195 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
1199 // start send thread
1201 res = CAQueueingThreadStart(&g_sendThread);
1203 res = CAQueueingThreadStart(&g_sendThread, "IoT_MessageHandlerQueue");
1205 if (CA_STATUS_OK != res)
1207 OIC_LOG(ERROR, TAG, "thread start error(send thread).");
1211 // receive thread initialize
1212 res = CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
1213 CAReceiveThreadProcess, CADestroyData);
1214 if (CA_STATUS_OK != res)
1216 OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
1220 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1221 // start receive thread
1222 res = CAQueueingThreadStart(&g_receiveThread);
1223 if (CA_STATUS_OK != res)
1225 OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
1228 #endif // SINGLE_HANDLE
1230 // retransmission initialize
1231 res = CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle,
1232 CASendUnicastData, CATimeoutCallback, NULL);
1233 if (CA_STATUS_OK != res)
1235 OIC_LOG(ERROR, TAG, "Failed to Initialize Retransmission.");
1240 // block-wise transfer initialize
1241 res = CAInitializeBlockWiseTransfer(CAAddDataToSendThread, CAAddDataToReceiveThread);
1242 if (CA_STATUS_OK != res)
1244 OIC_LOG(ERROR, TAG, "Failed to Initialize BlockWiseTransfer.");
1249 // start retransmission
1250 res = CARetransmissionStart(&g_retransmissionContext);
1251 if (CA_STATUS_OK != res)
1253 OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
1257 // initialize interface adapters by controller
1258 CAInitializeAdapters(g_threadPoolHandle, transportType);
1259 CASetNetworkMonitorCallbacks(CAAdapterStateChangedCallback, CAConnectionStateChangedCallback);
1261 // retransmission initialize
1262 CAResult_t res = CARetransmissionInitialize(&g_retransmissionContext, NULL, CASendUnicastData,
1263 CATimeoutCallback, NULL);
1264 if (CA_STATUS_OK != res)
1266 OIC_LOG(ERROR, TAG, "Failed to Initialize Retransmission.");
1270 CAInitializeAdapters();
1271 #endif // SINGLE_THREAD
1273 return CA_STATUS_OK;
1276 static bool CAClearQueueAdapterDataContext(void *data, uint32_t size, void *ctx)
1280 if (NULL == data || NULL == ctx)
1285 CAData_t *caData = (CAData_t *)data;
1286 CATransportAdapter_t *type = (CATransportAdapter_t *)ctx;
1288 if (NULL != caData && NULL != caData->remoteEndpoint
1289 && caData->remoteEndpoint->adapter == *type)
1296 void CAClearMessageHandler(CATransportAdapter_t transportType)
1298 CATransportAdapter_t *typeCtx = &transportType;
1300 CAResult_t res = CAQueueingThreadClearContextData(&g_sendThread,
1301 CAClearQueueAdapterDataContext,
1304 if (res != CA_STATUS_OK)
1306 OIC_LOG_V(ERROR, TAG, "Clear send data failed[%d]", res);
1309 if (transportType & DEFAULT_RETRANSMISSION_TYPE)
1311 res = CARetransmissionClearAdapterData(&g_retransmissionContext, transportType);
1312 if (res != CA_STATUS_OK)
1314 OIC_LOG_V(ERROR, TAG, "Clear retransmission data failed[%d]", res);
1319 void CATerminateMessageHandler()
1321 #ifndef SINGLE_THREAD
1322 CATransportAdapter_t connType;
1323 u_arraylist_t *list = CAGetSelectedNetworkList();
1324 uint32_t length = u_arraylist_length(list);
1327 for (i = 0; i < length; i++)
1329 void* ptrType = u_arraylist_get(list, i);
1331 if (NULL == ptrType)
1336 connType = *(CATransportAdapter_t *)ptrType;
1337 CAStopAdapter(connType);
1340 // stop retransmission
1341 if (NULL != g_retransmissionContext.threadMutex)
1343 CARetransmissionStop(&g_retransmissionContext);
1347 // delete thread data
1348 if (NULL != g_sendThread.threadMutex)
1350 CAQueueingThreadStop(&g_sendThread);
1354 // delete thread data
1355 if (NULL != g_receiveThread.threadMutex)
1357 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1358 CAQueueingThreadStop(&g_receiveThread);
1362 // destroy thread pool
1363 if (NULL != g_threadPoolHandle)
1365 ca_thread_pool_free(g_threadPoolHandle);
1366 g_threadPoolHandle = NULL;
1370 CATerminateBlockWiseTransfer();
1372 CARetransmissionDestroy(&g_retransmissionContext);
1373 CAQueueingThreadDestroy(&g_sendThread);
1374 CAQueueingThreadDestroy(&g_receiveThread);
1376 // terminate interface adapters by controller
1377 CATerminateAdapters();
1379 // terminate interface adapters by controller
1380 CATerminateAdapters();
1382 // stop retransmission
1383 CARetransmissionStop(&g_retransmissionContext);
1384 CARetransmissionDestroy(&g_retransmissionContext);
1385 #endif // SINGLE_THREAD
1388 static void CALogPayloadInfo(CAInfo_t *info)
1394 for (uint32_t i = 0; i < info->numOptions; i++)
1396 OIC_LOG_V(DEBUG, TAG, "optionID: %u", info->options[i].optionID);
1398 OIC_LOG_V(DEBUG, TAG, "list: %s", info->options[i].optionData);
1404 OIC_LOG_V(DEBUG, TAG, "payload: %p(%zu)", info->payload,
1410 OIC_LOG(DEBUG, TAG, "token:");
1411 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) info->token,
1414 OIC_LOG_V(DEBUG, TAG, "msgID: %u", info->messageId);
1418 OIC_LOG(DEBUG, TAG, "info is NULL, cannot output log data");
1422 void CAErrorHandler(const CAEndpoint_t *endpoint,
1423 const void *data, uint32_t dataLen,
1426 OIC_LOG(DEBUG, TAG, "CAErrorHandler IN");
1427 VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
1428 VERIFY_NON_NULL_VOID(data, TAG, "data");
1432 OIC_LOG(ERROR, TAG, "dataLen is zero");
1436 #ifndef SINGLE_THREAD
1437 uint32_t code = CA_NOT_FOUND;
1438 //Do not free remoteEndpoint and data. Currently they will be freed in data thread
1440 coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code, endpoint);
1443 OIC_LOG(ERROR, TAG, "Parse PDU failed");
1447 CAData_t *cadata = CAGenerateHandlerData(endpoint, NULL, pdu, CA_ERROR_DATA);
1450 OIC_LOG(ERROR, TAG, "CAErrorHandler, CAGenerateHandlerData failed!");
1451 coap_delete_pdu(pdu);
1455 cadata->errorInfo->result = result;
1457 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1458 coap_delete_pdu(pdu);
1463 OIC_LOG(DEBUG, TAG, "CAErrorHandler OUT");
1467 static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info, CAResult_t result)
1469 OIC_LOG(DEBUG, TAG, "CASendErrorInfo IN");
1470 #ifndef SINGLE_THREAD
1471 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
1474 OIC_LOG(ERROR, TAG, "cadata memory allocation failed");
1478 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
1481 OIC_LOG(ERROR, TAG, "endpoint clone failed");
1486 CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
1489 OIC_LOG(ERROR, TAG, "errorInfo memory allocation failed");
1495 CAResult_t res = CACloneInfo(info, &errorInfo->info);
1496 if (CA_STATUS_OK != res)
1498 OIC_LOG(ERROR, TAG, "info clone failed");
1505 errorInfo->result = result;
1506 cadata->remoteEndpoint = ep;
1507 cadata->errorInfo = errorInfo;
1508 cadata->dataType = CA_ERROR_DATA;
1510 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1512 OIC_LOG(DEBUG, TAG, "CASendErrorInfo OUT");
1519 static void CALogPDUInfo(const CAData_t *data, const coap_pdu_t *pdu)
1522 if(data == NULL || pdu == NULL)
1524 printf("INVALID INPUT, CALogPDUInfo FAIL\n");
1529 switch(data->dataType)
1531 case CA_REQUEST_DATA:
1532 strncpy(type, "\e[32mREQUEST <<<<\e[m", 30);
1534 case CA_RESPONSE_DATA:
1535 strncpy(type, "\e[36mRESPONSE >>>>\e[m", 30);
1538 strncpy(type, "ERROR", 30);
1540 case CA_RESPONSE_FOR_RES:
1541 strncpy(type, "RESP_RES >>>>", 30);
1544 snprintf(type, 30, "Type : %d", data->dataType);
1549 char method[20] = "";
1550 const CAInfo_t *info = NULL;
1551 if (NULL != data->requestInfo)
1553 switch(data->requestInfo->method)
1556 strncpy(method, "GET", 20);
1559 strncpy(method, "POST", 20);
1562 strncpy(method, "PUT", 20);
1565 strncpy(method, "DEL", 20);
1568 sprintf(method, "Method : %d", data->requestInfo->method);
1571 info = &data->requestInfo->info;
1574 if(NULL != data->responseInfo)
1577 sprintf(method, "result : %d", data->responseInfo->result);
1578 info = &data->responseInfo->info;
1582 char log_buffer[1024] = "";
1583 sprintf(log_buffer, "CA_LOG [%5d] | %-13s | %-12s | msg size : %4d | %s", pdu->transport_hdr->udp.id , type, method, pdu->length, info->resourceUri);
1591 static void CALogPDUInfo(const CAData_t *data, const coap_pdu_t *pdu)
1593 OIC_LOG(DEBUG, TAG, "CALogPDUInfo");
1595 VERIFY_NON_NULL_VOID(data, TAG, "data");
1596 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
1597 OIC_TRACE_BEGIN(%s:CALogPDUInfo, TAG);
1599 OIC_LOG(INFO, ANALYZER_TAG, "=================================================");
1600 if(SEND_TYPE_MULTICAST == data->type)
1602 OIC_LOG(INFO, ANALYZER_TAG, "Is Multicast = true");
1606 OIC_LOG(INFO, ANALYZER_TAG, "Is Multicast = false");
1609 if (NULL != data->remoteEndpoint)
1611 CALogAdapterTypeInfo(data->remoteEndpoint->adapter);
1612 OIC_LOG_V(DEBUG, ANALYZER_TAG, "Address = [%s]:[%d]", data->remoteEndpoint->addr,
1613 data->remoteEndpoint->port);
1616 switch(data->dataType)
1618 case CA_REQUEST_DATA:
1619 OIC_LOG(INFO, ANALYZER_TAG, "Data Type = [CA_REQUEST_DATA]");
1621 case CA_RESPONSE_DATA:
1622 OIC_LOG(INFO, ANALYZER_TAG, "Data Type = [CA_RESPONSE_DATA]");
1625 OIC_LOG(INFO, ANALYZER_TAG, "Data Type = [CA_ERROR_DATA]");
1627 case CA_RESPONSE_FOR_RES:
1628 OIC_LOG(INFO, ANALYZER_TAG, "Data Type = [CA_RESPONSE_FOR_RES]");
1631 OIC_LOG_V(INFO, ANALYZER_TAG, "Data Type = [%d]", data->dataType);
1635 const CAInfo_t *info = NULL;
1636 if (NULL != data->requestInfo)
1638 switch(data->requestInfo->method)
1641 OIC_LOG(INFO, ANALYZER_TAG, "Method = [GET]");
1644 OIC_LOG(INFO, ANALYZER_TAG, "Method = [POST]");
1647 OIC_LOG(INFO, ANALYZER_TAG, "Method = [PUT]");
1650 OIC_LOG(INFO, ANALYZER_TAG, "Method = [DELETE]");
1653 OIC_LOG_V(INFO, ANALYZER_TAG, "Method = [%d]", data->requestInfo->method);
1656 info = &data->requestInfo->info;
1659 if (NULL != data->responseInfo)
1661 OIC_LOG_V(INFO, ANALYZER_TAG, "result code = [%d]", data->responseInfo->result);
1662 info = &data->responseInfo->info;
1665 if (pdu->transport_hdr)
1667 OIC_LOG_V(INFO, ANALYZER_TAG, "Msg ID = [%d]", pdu->transport_hdr->udp.id);
1672 OIC_LOG(INFO, ANALYZER_TAG, "Coap Token");
1673 OIC_LOG_BUFFER(INFO, ANALYZER_TAG, (const uint8_t *) info->token, info->tokenLength);
1674 OIC_TRACE_BUFFER("OIC_CA_MSG_HANDLE:CALogPDUInfo:token",
1675 (const uint8_t *) info->token, info->tokenLength);
1676 OIC_LOG_V(INFO_PRIVATE, ANALYZER_TAG, "Res URI = [%s]", info->resourceUri);
1677 OIC_TRACE_MARK(%s:CALogPDUInfo:uri:%s, TAG, info->resourceUri);
1679 if (CA_FORMAT_APPLICATION_CBOR == info->payloadFormat)
1681 OIC_LOG(INFO, ANALYZER_TAG, "Payload Format = [CA_FORMAT_APPLICATION_CBOR]");
1685 OIC_LOG_V(INFO, ANALYZER_TAG, "Payload Format = [%d]", info->payloadFormat);
1689 size_t payloadLen = (pdu->data) ? (unsigned char *) pdu->hdr + pdu->length - pdu->data : 0;
1690 OIC_LOG_V(INFO, ANALYZER_TAG, "CoAP Message Full Size = [%u]", pdu->length);
1691 OIC_LOG(INFO, ANALYZER_TAG, "CoAP Header (+ 0xFF)");
1692 OIC_LOG_BUFFER(INFO, ANALYZER_TAG, (const uint8_t *) pdu->transport_hdr,
1693 pdu->length - payloadLen);
1694 OIC_LOG_V(INFO, ANALYZER_TAG, "CoAP Header size = [%" PRIuPTR "]", (size_t) pdu->length - payloadLen);
1696 OIC_LOG_V(INFO, ANALYZER_TAG, "CoAP Payload");
1697 OIC_LOG_BUFFER(INFO_PRIVATE, ANALYZER_TAG, pdu->data, payloadLen);
1698 OIC_LOG_V(INFO, ANALYZER_TAG, "CoAP Payload Size = [%" PRIuPTR "]", payloadLen);
1699 OIC_LOG(INFO, ANALYZER_TAG, "=================================================");
1702 CASamsungLogMessage(data, pdu);
1706 static void CASamsungLogMessage(const CAData_t *data, const coap_pdu_t *pdu)
1708 OIC_LOG(INFO, TAG, "CASamsungLogMessage");
1709 VERIFY_NON_NULL_VOID(data, TAG, "data");
1710 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
1711 VERIFY_NON_NULL_VOID(data->remoteEndpoint, TAG, "data->remoteEndpoint");
1713 const CAInfo_t *info = NULL;
1714 if (NULL != data->requestInfo)
1716 info = &data->requestInfo->info;
1719 if (NULL != data->responseInfo)
1721 info = &data->responseInfo->info;
1724 VERIFY_NON_NULL_VOID(info, TAG, "info");
1726 memset(g_headerBuffer, 0, MAX_LOG_BUFFER_SIZE);
1729 g_headerBuffer[g_headerIndex++] = data->dataType;
1730 g_headerBuffer[g_headerIndex++] = '|';
1731 g_headerBuffer[g_headerIndex++] = data->remoteEndpoint->adapter;
1732 g_headerBuffer[g_headerIndex++] = '|';
1733 g_headerBuffer[g_headerIndex++] = data->type;
1734 g_headerBuffer[g_headerIndex++] = '|';
1736 if (NULL != data->remoteEndpoint)
1739 while (data->remoteEndpoint->addr[i])
1741 g_headerBuffer[g_headerIndex++] = data->remoteEndpoint->addr[i];
1744 g_headerBuffer[g_headerIndex++] = ':';
1745 g_headerBuffer[g_headerIndex++] = (data->remoteEndpoint->port >> 8) & 0x0000ff;
1746 g_headerBuffer[g_headerIndex++] = data->remoteEndpoint->port & 0x000000ff;
1749 g_headerBuffer[g_headerIndex++] = '|';
1750 if (data->requestInfo)
1752 g_headerBuffer[g_headerIndex++] = data->requestInfo->method;
1756 g_headerBuffer[g_headerIndex++] = 0;
1759 g_headerBuffer[g_headerIndex++] = '|';
1760 if (data->responseInfo)
1762 g_headerBuffer[g_headerIndex++] = data->responseInfo->result;
1766 g_headerBuffer[g_headerIndex++] = 0;
1768 g_headerBuffer[g_headerIndex++] = '|';
1770 if (pdu->transport_hdr)
1772 g_headerBuffer[g_headerIndex++] = (pdu->transport_hdr->udp.id >> 8) & 0x0000ff;
1773 g_headerBuffer[g_headerIndex++] = pdu->transport_hdr->udp.id & 0x000000ff;
1777 g_headerBuffer[g_headerIndex++] = 0;
1778 g_headerBuffer[g_headerIndex++] = 0;
1780 g_headerBuffer[g_headerIndex++] = '|';
1782 if (info->token && info->tokenLength > 0)
1784 for (size_t i = 0; i < info->tokenLength; i++)
1786 g_headerBuffer[g_headerIndex++] = info->token[i];
1788 g_headerBuffer[g_headerIndex++] = '|';
1791 if (info->resourceUri)
1794 while (info->resourceUri[i])
1796 g_headerBuffer[g_headerIndex++] = info->resourceUri[i];
1799 g_headerBuffer[g_headerIndex++] = '|';
1802 OIC_LOG_CA_BUFFER(INFO, TAG, (uint8_t *) g_headerBuffer, g_headerIndex, 1);
1803 size_t payloadLen = (unsigned char *) pdu->hdr + pdu->length - pdu->data;
1804 OIC_LOG_CA_BUFFER(INFO_PRIVATE, TAG, pdu->data, payloadLen, 0);
1809 static void CALogPDUInfo(const CAData_t *data, const coap_pdu_t *pdu)
1811 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
1814 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
1815 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->transport_hdr->udp.type);
1816 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->transport_hdr->udp.code);
1817 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
1818 OIC_LOG_BUFFER(DEBUG, TAG, pdu->transport_hdr->udp.token,
1819 pdu->transport_hdr->udp.token_length);