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;
63 #ifdef WITH_PROCESS_EVENT
64 static oc_event g_processEvent = NULL;
65 #endif // WITH_PROCESS_EVENT
68 #define CA_MAX_RT_ARRAY_SIZE 3
69 #endif // SINGLE_THREAD
71 #define TAG "OIC_CA_MSG_HANDLE"
73 static CARetransmission_t g_retransmissionContext;
76 static CARequestCallback g_requestHandler = NULL;
77 static CAResponseCallback g_responseHandler = NULL;
78 static CAErrorCallback g_errorHandler = NULL;
79 static CANetworkMonitorCallback g_nwMonitorHandler = NULL;
81 static void CAErrorHandler(const CAEndpoint_t *endpoint,
82 const void *data, uint32_t dataLen,
85 static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint,
86 const CARemoteId_t *identity,
87 const void *data, CADataType_t dataType);
89 static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info,
93 static void CAProcessReceivedData(CAData_t *data);
95 static void CADestroyData(void *data, uint32_t size);
96 static void CALogPayloadInfo(CAInfo_t *info);
97 static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *endpoint, uint16_t id,
98 CAToken_t token, uint8_t tokenLength);
101 * print send / receive message of CoAP.
102 * @param[in] data CA information which has send/receive message and endpoint.
103 * @param[in] pdu CoAP pdu low data.
105 static void CALogPDUInfo(const CAData_t *data, const coap_pdu_t *pdu);
108 static char g_headerBuffer[MAX_LOG_BUFFER_SIZE] = {0};
109 static size_t g_headerIndex = 0;
110 static void CASamsungLogMessage(const CAData_t *data, const coap_pdu_t *pdu);
114 void CAAddDataToSendThread(CAData_t *data)
116 VERIFY_NON_NULL_VOID(data, TAG, "data");
119 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
122 void CAAddDataToReceiveThread(CAData_t *data)
124 VERIFY_NON_NULL_VOID(data, TAG, "data");
127 CAQueueingThreadAddData(&g_receiveThread, data, sizeof(CAData_t));
129 #ifdef WITH_PROCESS_EVENT
132 oc_event_signal(g_processEvent);
138 static bool CAIsSelectedNetworkAvailable()
140 u_arraylist_t *list = CAGetSelectedNetworkList();
141 if (!list || u_arraylist_length(list) == 0)
143 OIC_LOG(ERROR, TAG, "No selected network");
150 static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint,
151 const CARemoteId_t *identity,
152 const void *data, CADataType_t dataType)
154 OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData IN");
155 CAInfo_t *info = NULL;
156 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
159 OIC_LOG(ERROR, TAG, "memory allocation failed");
163 CAEndpoint_t* ep = endpoint;
165 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
168 OIC_LOG(ERROR, TAG, "endpoint clone failed");
173 OIC_LOG_V(DEBUG, TAG, "address : %s", ep->addr);
175 if (CA_RESPONSE_DATA == dataType)
177 CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
180 OIC_LOG(ERROR, TAG, "memory allocation failed");
184 CAResult_t result = CAGetResponseInfoFromPDU(data, resInfo, endpoint);
185 if (CA_STATUS_OK != result)
187 OIC_LOG(ERROR, TAG, "CAGetResponseInfoFromPDU Failed");
188 CADestroyResponseInfoInternal(resInfo);
191 cadata->responseInfo = resInfo;
192 info = &resInfo->info;
195 info->identity = *identity;
197 OIC_LOG(DEBUG, TAG, "Response Info :");
198 CALogPayloadInfo(info);
200 else if (CA_REQUEST_DATA == dataType)
202 CARequestInfo_t* reqInfo = (CARequestInfo_t*)OICCalloc(1, sizeof(CARequestInfo_t));
205 OIC_LOG(ERROR, TAG, "memory allocation failed");
209 CAResult_t result = CAGetRequestInfoFromPDU(data, endpoint, reqInfo);
210 if (CA_STATUS_OK != result)
212 OIC_LOG(ERROR, TAG, "CAGetRequestInfoFromPDU failed");
213 CADestroyRequestInfoInternal(reqInfo);
217 if ((reqInfo->info.type != CA_MSG_CONFIRM) &&
218 CADropSecondMessage(&caglobals.ca.requestHistory, endpoint, reqInfo->info.messageId,
219 reqInfo->info.token, reqInfo->info.tokenLength))
221 OIC_LOG(INFO, TAG, "Second Request with same Token, Drop it");
222 CADestroyRequestInfoInternal(reqInfo);
226 cadata->requestInfo = reqInfo;
227 info = &reqInfo->info;
230 info->identity = *identity;
232 OIC_LOG(DEBUG, TAG, "Request Info :");
233 CALogPayloadInfo(info);
235 else if (CA_ERROR_DATA == dataType)
237 CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
240 OIC_LOG(ERROR, TAG, "Memory allocation failed!");
244 CAResult_t result = CAGetErrorInfoFromPDU(data, endpoint, errorInfo);
245 if (CA_STATUS_OK != result)
247 OIC_LOG(ERROR, TAG, "CAGetErrorInfoFromPDU failed");
252 cadata->errorInfo = errorInfo;
253 info = &errorInfo->info;
256 info->identity = *identity;
258 OIC_LOG(DEBUG, TAG, "error Info :");
259 CALogPayloadInfo(info);
262 cadata->remoteEndpoint = ep;
263 cadata->dataType = dataType;
265 OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData OUT");
270 #ifndef SINGLE_THREAD
276 static void CATimeoutCallback(const CAEndpoint_t *endpoint, const void *pdu, uint32_t size)
278 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
279 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
281 CAEndpoint_t* ep = endpoint;
283 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
286 OIC_LOG(ERROR, TAG, "clone failed");
291 CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
295 OIC_LOG(ERROR, TAG, "calloc failed");
296 #ifndef SINGLE_THREAD
302 resInfo->result = CA_RETRANSMIT_TIMEOUT;
303 resInfo->info.type = CAGetMessageTypeFromPduBinaryData(pdu, size);
304 resInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
306 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_transport_t *) pdu, &(resInfo->info),
308 if (CA_STATUS_OK != res)
310 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
311 CADestroyResponseInfoInternal(resInfo);
312 #ifndef SINGLE_THREAD
318 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
321 OIC_LOG(ERROR, TAG, "memory allocation failed !");
322 #ifndef SINGLE_THREAD
325 CADestroyResponseInfoInternal(resInfo);
329 cadata->type = SEND_TYPE_UNICAST;
330 cadata->remoteEndpoint = ep;
331 cadata->requestInfo = NULL;
332 cadata->responseInfo = resInfo;
335 if (CAIsSupportedBlockwiseTransfer(endpoint->adapter))
337 res = CARemoveBlockDataFromListWithSeed(resInfo->info.token, resInfo->info.tokenLength,
339 if (CA_STATUS_OK != res)
341 OIC_LOG(ERROR, TAG, "CARemoveBlockDataFromListWithSeed failed");
347 CAProcessReceivedData(cadata);
349 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
351 #ifdef WITH_PROCESS_EVENT
354 oc_event_signal(g_processEvent);
356 #endif//WITH_PROCESS_EVENT
357 #endif// SINGLE_THREAD
360 static void CADestroyData(void *data, uint32_t size)
362 OIC_LOG(DEBUG, TAG, "CADestroyData IN");
363 if ((size_t)size < sizeof(CAData_t))
365 OIC_LOG_V(ERROR, TAG, "Destroy data too small %p %d", data, size);
367 CAData_t *cadata = (CAData_t *) data;
371 OIC_LOG(ERROR, TAG, "cadata is NULL");
374 #ifndef SINGLE_THREAD
375 if (NULL != cadata->remoteEndpoint)
377 CAFreeEndpoint(cadata->remoteEndpoint);
381 if (NULL != cadata->requestInfo)
383 CADestroyRequestInfoInternal((CARequestInfo_t *) cadata->requestInfo);
386 if (NULL != cadata->responseInfo)
388 CADestroyResponseInfoInternal((CAResponseInfo_t *) cadata->responseInfo);
391 if (NULL != cadata->errorInfo)
393 CADestroyErrorInfoInternal(cadata->errorInfo);
397 OIC_LOG(DEBUG, TAG, "CADestroyData OUT");
401 static void CAProcessReceivedData(CAData_t *data)
403 OIC_LOG(DEBUG, TAG, "CAProcessReceivedData IN");
406 OIC_LOG(ERROR, TAG, "thread data error!!");
410 // parse the data and call the callbacks.
413 CAEndpoint_t *rep = (CAEndpoint_t *)(data->remoteEndpoint);
416 OIC_LOG(ERROR, TAG, "remoteEndpoint error!!");
420 if (data->requestInfo && g_requestHandler)
422 g_requestHandler(rep, data->requestInfo);
424 else if (data->responseInfo && g_responseHandler)
426 g_responseHandler(rep, data->responseInfo);
428 else if (data->errorInfo && g_errorHandler)
430 g_errorHandler(rep, data->errorInfo);
433 CADestroyData(data, sizeof(CAData_t));
435 OIC_LOG(DEBUG, TAG, "CAProcessReceivedData OUT");
439 #ifndef SINGLE_THREAD
440 static void CAReceiveThreadProcess(void *threadData)
442 #ifndef SINGLE_HANDLE
443 CAData_t *data = (CAData_t *) threadData;
444 OIC_TRACE_BEGIN(%s:CAProcessReceivedData, TAG);
445 CAProcessReceivedData(data);
451 #endif // SINGLE_THREAD
453 static CAResult_t CAProcessMulticastData(const CAData_t *data)
455 VERIFY_NON_NULL(data, TAG, "data");
456 VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");
458 coap_pdu_t *pdu = NULL;
459 CAInfo_t *info = NULL;
460 coap_list_t *options = NULL;
461 coap_transport_t transport = COAP_UDP;
462 CAResult_t res = CA_SEND_FAILED;
464 if (!data->requestInfo && !data->responseInfo)
466 OIC_LOG(ERROR, TAG, "request or response info is empty");
470 if (data->requestInfo)
472 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
474 info = &data->requestInfo->info;
475 pdu = CAGeneratePDU(CA_GET, info, data->remoteEndpoint, &options, &transport);
477 else if (data->responseInfo)
479 OIC_LOG(DEBUG, TAG, "responseInfo is available..");
481 info = &data->responseInfo->info;
482 pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint,
483 &options, &transport);
488 OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
489 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
490 coap_delete_list(options);
495 if (CAIsSupportedBlockwiseTransfer(data->remoteEndpoint->adapter))
497 // Blockwise transfer
498 res = CAAddBlockOption(&pdu, info, data->remoteEndpoint, &options);
499 if (CA_STATUS_OK != res)
501 OIC_LOG(DEBUG, TAG, "CAAddBlockOption has failed");
507 CALogPDUInfo(data, pdu);
509 res = CASendMulticastData(data->remoteEndpoint, pdu->transport_hdr, pdu->length, data->dataType);
510 if (CA_STATUS_OK != res)
512 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
516 coap_delete_list(options);
517 coap_delete_pdu(pdu);
521 CAErrorHandler(data->remoteEndpoint, pdu->transport_hdr, pdu->length, res);
522 coap_delete_list(options);
523 coap_delete_pdu(pdu);
527 static CAResult_t CAProcessSendData(const CAData_t *data)
529 VERIFY_NON_NULL(data, TAG, "data");
530 VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");
532 CAResult_t res = CA_STATUS_FAILED;
534 if (CA_NETWORK_COMMAND == data->dataType)
536 if (CA_REQ_DISCONNECT == data->eventInfo)
539 // request TCP disconnect
540 if (CA_ADAPTER_TCP == data->remoteEndpoint->adapter)
542 OIC_LOG(INFO, TAG, "request TCP disconnect");
543 return CADisconnectSession(data->remoteEndpoint);
549 CASendDataType_t type = data->type;
551 coap_pdu_t *pdu = NULL;
552 CAInfo_t *info = NULL;
553 coap_list_t *options = NULL;
554 coap_transport_t transport = COAP_UDP;
556 if (SEND_TYPE_UNICAST == type)
558 OIC_LOG(DEBUG,TAG,"Unicast message");
560 #ifdef ROUTING_GATEWAY
562 * When forwarding a packet, do not attempt retransmission as its the responsibility of
563 * packet originator node
565 bool skipRetransmission = false;
568 if (NULL != data->requestInfo)
570 OIC_LOG(DEBUG, TAG, "requestInfo is available..");
572 info = &data->requestInfo->info;
573 #ifdef ROUTING_GATEWAY
574 skipRetransmission = data->requestInfo->info.skipRetransmission;
576 pdu = CAGeneratePDU(data->requestInfo->method, info, data->remoteEndpoint,
577 &options, &transport);
579 else if (NULL != data->responseInfo)
581 OIC_LOG(DEBUG, TAG, "responseInfo is available..");
583 info = &data->responseInfo->info;
584 #ifdef ROUTING_GATEWAY
585 skipRetransmission = data->responseInfo->info.skipRetransmission;
587 pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint,
588 &options, &transport);
592 OIC_LOG(DEBUG, TAG, "request info, response info is empty");
593 return CA_STATUS_INVALID_PARAM;
596 // interface controller function call.
600 if (CAIsSupportedBlockwiseTransfer(data->remoteEndpoint->adapter))
602 // Blockwise transfer
605 CAResult_t res = CAAddBlockOption(&pdu, info,
606 data->remoteEndpoint,
608 if (CA_STATUS_OK != res)
610 OIC_LOG(INFO, TAG, "to write block option has failed");
611 CAErrorHandler(data->remoteEndpoint, pdu->transport_hdr, pdu->length, res);
612 coap_delete_list(options);
613 coap_delete_pdu(pdu);
619 CALogPDUInfo(data, pdu);
621 OIC_LOG_V(INFO, TAG, "CASendUnicastData type : %d", data->dataType);
622 res = CASendUnicastData(data->remoteEndpoint, pdu->transport_hdr, pdu->length, data->dataType);
623 if (CA_STATUS_OK != res)
625 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
626 CAErrorHandler(data->remoteEndpoint, pdu->transport_hdr, pdu->length, res);
627 coap_delete_list(options);
628 coap_delete_pdu(pdu);
633 if (CAIsSupportedCoAPOverTCP(data->remoteEndpoint->adapter))
635 OIC_LOG(INFO, TAG, "retransmission will be not worked");
639 #ifdef ROUTING_GATEWAY
640 if (!skipRetransmission)
643 // for retransmission
644 res = CARetransmissionSentData(&g_retransmissionContext,
645 data->remoteEndpoint,
647 pdu->transport_hdr, pdu->length);
648 if ((CA_STATUS_OK != res) && (CA_NOT_SUPPORTED != res))
650 //when retransmission not supported this will return CA_NOT_SUPPORTED, ignore
651 OIC_LOG_V(INFO, TAG, "retransmission is not enabled due to error, res : %d", res);
652 coap_delete_list(options);
653 coap_delete_pdu(pdu);
658 coap_delete_list(options);
659 coap_delete_pdu(pdu);
663 OIC_LOG(ERROR,TAG,"Failed to generate unicast PDU");
664 CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
665 return CA_SEND_FAILED;
668 else if (SEND_TYPE_MULTICAST == type)
670 OIC_LOG(DEBUG,TAG,"Multicast message");
673 * If CoAP over TCP is enabled, the CoAP pdu wont be same for IP and other adapters.
674 * That's why we need to generate two pdu's, one for IP and second for other transports.
675 * Two possible cases we might have to split: a) when adapter is CA_DEFAULT_ADAPTER
676 * b) when one of the adapter is IP adapter(ex: CA_ADAPTER_IP | CA_ADAPTER_GATT_BTLE)
678 if (data->remoteEndpoint->adapter == CA_DEFAULT_ADAPTER ||
679 (CA_ADAPTER_IP & data->remoteEndpoint->adapter &&
680 CA_ADAPTER_IP != data->remoteEndpoint->adapter))
682 if (data->remoteEndpoint->adapter == CA_DEFAULT_ADAPTER)
684 data->remoteEndpoint->adapter = CA_ALL_ADAPTERS ^ CA_ADAPTER_IP;
688 data->remoteEndpoint->adapter = data->remoteEndpoint->adapter ^ CA_ADAPTER_IP;
690 CAProcessMulticastData(data);
691 data->remoteEndpoint->adapter = CA_ADAPTER_IP;
692 CAProcessMulticastData(data);
696 CAProcessMulticastData(data);
699 CAProcessMulticastData(data);
705 #ifndef SINGLE_THREAD
706 static void CASendThreadProcess(void *threadData)
708 CAData_t *data = (CAData_t *) threadData;
709 OIC_TRACE_BEGIN(%s:CAProcessSendData, TAG);
710 CAProcessSendData(data);
716 * If a second message arrives with the same message ID, token and the other address
717 * family, drop it. Typically, IPv6 beats IPv4, so the IPv4 message is dropped.
719 static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *ep, uint16_t id,
720 CAToken_t token, uint8_t tokenLength)
726 if (ep->adapter != CA_ADAPTER_IP)
730 if (!caglobals.ip.dualstack)
735 if (tokenLength > CA_MAX_TOKEN_LEN)
738 * If token length is more than CA_MAX_TOKEN_LEN,
739 * we compare the first CA_MAX_TOKEN_LEN bytes only.
741 tokenLength = CA_MAX_TOKEN_LEN;
745 CATransportFlags_t familyFlags = ep->flags & CA_IPFAMILY_MASK;
747 for (size_t i = 0; i < sizeof(history->items) / sizeof(history->items[0]); i++)
749 CAHistoryItem_t *item = &(history->items[i]);
750 if (id == item->messageId && tokenLength == item->tokenLength
751 && memcmp(item->token, token, tokenLength) == 0)
753 if ((familyFlags ^ item->flags) == CA_IPFAMILY_MASK)
755 OIC_LOG_V(INFO, TAG, "IPv%c duplicate message ignored",
756 familyFlags & CA_IPV6 ? '6' : '4');
763 history->items[history->nextIndex].flags = familyFlags;
764 history->items[history->nextIndex].messageId = id;
765 if (token && tokenLength)
767 memcpy(history->items[history->nextIndex].token, token, tokenLength);
768 history->items[history->nextIndex].tokenLength = tokenLength;
771 if (++history->nextIndex >= HISTORYSIZE)
773 history->nextIndex = 0;
779 static CAResult_t CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
780 const void *data, uint32_t dataLen)
782 VERIFY_NON_NULL(sep, TAG, "remoteEndpoint");
783 VERIFY_NON_NULL(data, TAG, "data");
784 OIC_TRACE_BEGIN(%s:CAReceivedPacketCallback, TAG);
788 OIC_LOG(ERROR, TAG, "dataLen is zero");
791 return CA_STATUS_FAILED;
795 OIC_LOG(DEBUG, TAG, "received pdu data :");
798 OIC_LOG_BUFFER(DEBUG, TAG, data, dataLen);
802 OIC_LOG_BUFFER(DEBUG, TAG, data, 32);
805 CAResult_t res = CA_STATUS_OK;
806 uint32_t code = CA_NOT_FOUND;
807 CAData_t *cadata = NULL;
809 coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code,
813 OIC_LOG(ERROR, TAG, "Parse PDU failed");
814 res = CA_STATUS_FAILED;
818 OIC_LOG_V(DEBUG, TAG, "code = %d", code);
819 if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
821 cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_REQUEST_DATA);
824 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
825 coap_delete_pdu(pdu);
831 cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_RESPONSE_DATA);
834 OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
835 coap_delete_pdu(pdu);
840 if (CAIsSupportedCoAPOverTCP(sep->endpoint.adapter))
842 OIC_LOG(INFO, TAG, "retransmission is not supported");
847 // for retransmission
848 void *retransmissionPdu = NULL;
849 CARetransmissionReceivedData(&g_retransmissionContext, cadata->remoteEndpoint, pdu->transport_hdr,
850 pdu->length, &retransmissionPdu);
852 // get token from saved data in retransmission list
853 if (retransmissionPdu && CA_EMPTY == code)
855 if (cadata->responseInfo)
857 CAInfo_t *info = &cadata->responseInfo->info;
858 CAResult_t res = CAGetTokenFromPDU((const coap_hdr_transport_t *)retransmissionPdu,
859 info, &(sep->endpoint));
860 if (CA_STATUS_OK != res)
862 OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
863 OICFree(info->token);
864 info->tokenLength = 0;
868 OICFree(retransmissionPdu);
872 cadata->type = SEND_TYPE_UNICAST;
874 CALogPDUInfo(cadata, pdu);
877 CAProcessReceivedData(cadata);
880 if (CAIsSupportedBlockwiseTransfer(sep->endpoint.adapter))
882 CAResult_t res = CAReceiveBlockWiseData(pdu, &(sep->endpoint), cadata, dataLen);
883 if (CA_NOT_SUPPORTED == res || CA_REQUEST_TIMEOUT == res)
885 OIC_LOG(DEBUG, TAG, "this message does not have block option");
886 CAAddDataToReceiveThread(cadata);
890 CADestroyData(cadata, sizeof(CAData_t));
896 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
898 #ifdef WITH_PROCESS_EVENT
901 oc_event_signal(g_processEvent);
905 #endif // SINGLE_THREAD
907 coap_delete_pdu(pdu);
910 OIC_LOG(DEBUG, TAG, "OUT - Recv Thread");
915 static void CAAdapterStateChangedCallback(CATransportAdapter_t transportType, bool enabled)
919 CAClearMessageHandler(transportType);
923 static bool CAClearQueueEndpointDataContext(void *data, uint32_t size, void *ctx)
926 if (NULL == data || NULL == ctx)
931 CAData_t *caData = (CAData_t *)data;
932 const CAEndpoint_t *endpoint = (const CAEndpoint_t *)ctx;
934 if (NULL != caData && NULL != caData->remoteEndpoint)
936 if (strcmp(caData->remoteEndpoint->addr, endpoint->addr) == 0
937 && caData->remoteEndpoint->port == endpoint->port
938 && caData->remoteEndpoint->adapter == endpoint->adapter)
946 static void CAConnectionStateChangedCallback(const CAEndpoint_t *info, bool isConnected)
950 CAResult_t res = CAQueueingThreadClearContextData(&g_sendThread,
951 CAClearQueueEndpointDataContext,
953 if (CA_STATUS_OK != res)
955 OIC_LOG(ERROR, TAG, "Could not clear the send queue");
960 static u_queue_message_t *get_receive_queue_item(void)
962 u_queue_message_t *item = NULL;
964 oc_mutex_lock(g_receiveThread.threadMutex);
965 item = u_queue_get_element(g_receiveThread.dataQueue);
966 oc_mutex_unlock(g_receiveThread.threadMutex);
972 void CAHandleRequestResponseCallbacks()
976 CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
979 // parse the data and call the callbacks.
983 u_queue_message_t *item = NULL;
984 #ifdef WITH_PROCESS_EVENT
985 while ((item = get_receive_queue_item()) != NULL)
987 if ((item = get_receive_queue_item()) != NULL)
989 { if (NULL == item->msg)
992 #ifdef WITH_PROCESS_EVENT
1000 CAData_t *td = (CAData_t *) item->msg;
1002 if (td->requestInfo && g_requestHandler)
1004 OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
1005 g_requestHandler(td->remoteEndpoint, td->requestInfo);
1007 else if (td->responseInfo && g_responseHandler)
1009 OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
1010 g_responseHandler(td->remoteEndpoint, td->responseInfo);
1012 else if (td->errorInfo && g_errorHandler)
1014 OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
1015 g_errorHandler(td->remoteEndpoint, td->errorInfo);
1018 CADestroyData(item->msg, sizeof(CAData_t));
1021 #endif // SINGLE_HANDLE
1022 #endif // SINGLE_THREAD
1025 static CAData_t* CAPrepareSendData(const CAEndpoint_t *endpoint, const void *sendData,
1026 CADataType_t dataType)
1028 OIC_LOG(DEBUG, TAG, "CAPrepareSendData IN");
1030 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
1033 OIC_LOG(ERROR, TAG, "memory allocation failed");
1037 if (CA_REQUEST_DATA == dataType)
1039 #ifdef SINGLE_THREAD
1040 CARequestInfo_t *request = (CARequestInfo_t *)sendData;
1042 // clone request info
1043 CARequestInfo_t *request = CACloneRequestInfo((CARequestInfo_t *)sendData);
1046 OIC_LOG(ERROR, TAG, "CACloneRequestInfo failed");
1050 cadata->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
1051 cadata->requestInfo = request;
1053 else if (CA_RESPONSE_DATA == dataType || CA_RESPONSE_FOR_RES == dataType)
1055 #ifdef SINGLE_THREAD
1056 CAResponseInfo_t *response = (CAResponseInfo_t *)sendData;
1058 // clone response info
1059 CAResponseInfo_t *response = CACloneResponseInfo((CAResponseInfo_t *)sendData);
1062 OIC_LOG(ERROR, TAG, "CACloneResponseInfo failed");
1066 cadata->type = response->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
1067 cadata->responseInfo = response;
1071 OIC_LOG(ERROR, TAG, "CAPrepareSendData unknown data type");
1075 #ifdef SINGLE_THREAD
1076 CAEndpoint_t* ep = endpoint;
1078 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
1081 OIC_LOG(ERROR, TAG, "endpoint clone failed");
1085 cadata->remoteEndpoint = ep;
1086 cadata->dataType = dataType;
1090 #ifndef SINGLE_THREAD
1091 CADestroyData(cadata, sizeof(CAData_t));
1098 CAResult_t CADetachSendNetworkReqMessage(const CAEndpoint_t *endpoint,
1099 CAConnectEvent_t event,
1100 CADataType_t dataType)
1102 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
1104 if (false == CAIsSelectedNetworkAvailable())
1106 return CA_STATUS_FAILED;
1109 #ifndef SINGLE_THREAD
1110 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
1113 OIC_LOG(ERROR, TAG, "cadata memory allocation failed");
1114 return CA_MEMORY_ALLOC_FAILED;
1117 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
1120 OIC_LOG(ERROR, TAG, "endpoint clone failed");
1122 return CA_MEMORY_ALLOC_FAILED;
1125 cadata->remoteEndpoint = ep;
1126 cadata->eventInfo = event;
1127 cadata->dataType = dataType;
1129 CAQueueingThreadAddData(&g_sendThread, cadata, sizeof(CAData_t));
1132 return CA_STATUS_OK;
1135 CAResult_t CADetachSendMessage(const CAEndpoint_t *endpoint, const void *sendMsg,
1136 CADataType_t dataType)
1138 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
1139 VERIFY_NON_NULL(sendMsg, TAG, "sendMsg");
1141 if (false == CAIsSelectedNetworkAvailable())
1143 return CA_STATUS_FAILED;
1147 // If max retransmission queue is reached, then don't handle new request
1148 if (CA_MAX_RT_ARRAY_SIZE == u_arraylist_length(g_retransmissionContext.dataList))
1150 OIC_LOG(ERROR, TAG, "max RT queue size reached!");
1151 return CA_SEND_FAILED;
1155 CAData_t *data = CAPrepareSendData(endpoint, sendMsg, dataType);
1158 OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
1159 return CA_MEMORY_ALLOC_FAILED;
1162 OIC_LOG_V(INFO_PRIVATE, TAG, "DID of endpoint of this message is %s", endpoint->remoteId);
1164 #ifdef SINGLE_THREAD
1165 CAResult_t result = CAProcessSendData(data);
1166 if (CA_STATUS_OK != result)
1168 OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
1177 if (CAIsSupportedBlockwiseTransfer(endpoint->adapter))
1179 CACheckAndDeleteTimedOutBlockData();
1181 CAResult_t res = CASendBlockWiseData(data);
1182 if (CA_NOT_SUPPORTED == res)
1184 OIC_LOG(DEBUG, TAG, "normal msg will be sent");
1185 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
1186 return CA_STATUS_OK;
1190 CADestroyData(data, sizeof(CAData_t));
1198 CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
1200 #endif // SINGLE_THREAD
1202 return CA_STATUS_OK;
1205 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
1206 CAErrorCallback errorHandler)
1208 g_requestHandler = ReqHandler;
1209 g_responseHandler = RespHandler;
1210 g_errorHandler = errorHandler;
1213 void CASetNetworkMonitorCallback(CANetworkMonitorCallback nwMonitorHandler)
1215 g_nwMonitorHandler = nwMonitorHandler;
1218 CAResult_t CAInitializeMessageHandler(CATransportAdapter_t transportType)
1220 CASetPacketReceivedCallback((CANetworkPacketReceivedCallback)CAReceivedPacketCallback);
1221 CASetErrorHandleCallback((CAErrorHandleCallback)CAErrorHandler);
1223 #ifndef SINGLE_THREAD
1224 // create thread pool
1225 CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
1226 if (CA_STATUS_OK != res)
1228 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
1232 // send thread initialize
1233 res = CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
1234 CASendThreadProcess, CADestroyData);
1235 if (CA_STATUS_OK != res)
1237 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
1241 // start send thread
1243 res = CAQueueingThreadStart(&g_sendThread);
1245 res = CAQueueingThreadStart(&g_sendThread, "IoT_MessageHandlerQueue");
1247 if (CA_STATUS_OK != res)
1249 OIC_LOG(ERROR, TAG, "thread start error(send thread).");
1253 // receive thread initialize
1254 res = CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
1255 CAReceiveThreadProcess, CADestroyData);
1256 if (CA_STATUS_OK != res)
1258 OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
1262 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1263 // start receive thread
1264 res = CAQueueingThreadStart(&g_receiveThread);
1265 if (CA_STATUS_OK != res)
1267 OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
1270 #endif // SINGLE_HANDLE
1272 // retransmission initialize
1273 res = CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle,
1274 CASendUnicastData, CATimeoutCallback, NULL);
1275 if (CA_STATUS_OK != res)
1277 OIC_LOG(ERROR, TAG, "Failed to Initialize Retransmission.");
1282 // block-wise transfer initialize
1283 res = CAInitializeBlockWiseTransfer(CAAddDataToSendThread, CAAddDataToReceiveThread);
1284 if (CA_STATUS_OK != res)
1286 OIC_LOG(ERROR, TAG, "Failed to Initialize BlockWiseTransfer.");
1291 // start retransmission
1292 res = CARetransmissionStart(&g_retransmissionContext);
1293 if (CA_STATUS_OK != res)
1295 OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
1299 // initialize interface adapters by controller
1300 CAInitializeAdapters(g_threadPoolHandle, transportType);
1301 CASetNetworkMonitorCallbacks(CAAdapterStateChangedCallback, CAConnectionStateChangedCallback);
1303 // retransmission initialize
1304 CAResult_t res = CARetransmissionInitialize(&g_retransmissionContext, NULL, CASendUnicastData,
1305 CATimeoutCallback, NULL);
1306 if (CA_STATUS_OK != res)
1308 OIC_LOG(ERROR, TAG, "Failed to Initialize Retransmission.");
1312 CAInitializeAdapters();
1313 #endif // SINGLE_THREAD
1315 return CA_STATUS_OK;
1318 static bool CAClearQueueAdapterDataContext(void *data, uint32_t size, void *ctx)
1322 if (NULL == data || NULL == ctx)
1327 CAData_t *caData = (CAData_t *)data;
1328 CATransportAdapter_t *type = (CATransportAdapter_t *)ctx;
1330 if (NULL != caData && NULL != caData->remoteEndpoint
1331 && caData->remoteEndpoint->adapter == *type)
1338 void CAClearMessageHandler(CATransportAdapter_t transportType)
1340 CATransportAdapter_t *typeCtx = &transportType;
1342 CAResult_t res = CAQueueingThreadClearContextData(&g_sendThread,
1343 CAClearQueueAdapterDataContext,
1346 if (res != CA_STATUS_OK)
1348 OIC_LOG_V(ERROR, TAG, "Clear send data failed[%d]", res);
1351 if (transportType & DEFAULT_RETRANSMISSION_TYPE)
1353 res = CARetransmissionClearAdapterData(&g_retransmissionContext, transportType);
1354 if (res != CA_STATUS_OK)
1356 OIC_LOG_V(ERROR, TAG, "Clear retransmission data failed[%d]", res);
1361 void CATerminateMessageHandler()
1363 #ifndef SINGLE_THREAD
1364 CATransportAdapter_t connType;
1365 u_arraylist_t *list = CAGetSelectedNetworkList();
1366 uint32_t length = u_arraylist_length(list);
1368 #ifdef WITH_PROCESS_EVENT
1369 g_processEvent = NULL;
1373 for (i = 0; i < length; i++)
1375 void* ptrType = u_arraylist_get(list, i);
1377 if (NULL == ptrType)
1382 connType = *(CATransportAdapter_t *)ptrType;
1383 CAStopAdapter(connType);
1386 // stop retransmission
1387 if (NULL != g_retransmissionContext.threadMutex)
1389 CARetransmissionStop(&g_retransmissionContext);
1393 // delete thread data
1394 if (NULL != g_sendThread.threadMutex)
1396 CAQueueingThreadStop(&g_sendThread);
1400 // delete thread data
1401 if (NULL != g_receiveThread.threadMutex)
1403 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1404 CAQueueingThreadStop(&g_receiveThread);
1408 // destroy thread pool
1409 if (NULL != g_threadPoolHandle)
1411 ca_thread_pool_free(g_threadPoolHandle);
1412 g_threadPoolHandle = NULL;
1416 CATerminateBlockWiseTransfer();
1418 CARetransmissionDestroy(&g_retransmissionContext);
1419 CAQueueingThreadDestroy(&g_sendThread);
1420 CAQueueingThreadDestroy(&g_receiveThread);
1422 // terminate interface adapters by controller
1423 CATerminateAdapters();
1425 // terminate interface adapters by controller
1426 CATerminateAdapters();
1428 // stop retransmission
1429 CARetransmissionStop(&g_retransmissionContext);
1430 CARetransmissionDestroy(&g_retransmissionContext);
1431 #endif // SINGLE_THREAD
1434 static void CALogPayloadInfo(CAInfo_t *info)
1440 for (uint32_t i = 0; i < info->numOptions; i++)
1442 OIC_LOG_V(DEBUG, TAG, "optionID: %u", info->options[i].optionID);
1444 OIC_LOG_V(DEBUG, TAG, "list: %s", info->options[i].optionData);
1450 OIC_LOG_V(DEBUG, TAG, "payload: %p(%zu)", info->payload,
1456 OIC_LOG(DEBUG, TAG, "token:");
1457 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) info->token,
1460 OIC_LOG_V(DEBUG, TAG, "msgID: %u", info->messageId);
1464 OIC_LOG(DEBUG, TAG, "info is NULL, cannot output log data");
1468 void CAErrorHandler(const CAEndpoint_t *endpoint,
1469 const void *data, uint32_t dataLen,
1472 OIC_LOG(DEBUG, TAG, "CAErrorHandler IN");
1473 VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
1474 VERIFY_NON_NULL_VOID(data, TAG, "data");
1478 OIC_LOG(ERROR, TAG, "dataLen is zero");
1482 #ifndef SINGLE_THREAD
1483 uint32_t code = CA_NOT_FOUND;
1484 //Do not free remoteEndpoint and data. Currently they will be freed in data thread
1486 coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code, endpoint);
1489 OIC_LOG(ERROR, TAG, "Parse PDU failed");
1493 CAData_t *cadata = CAGenerateHandlerData(endpoint, NULL, pdu, CA_ERROR_DATA);
1496 OIC_LOG(ERROR, TAG, "CAErrorHandler, CAGenerateHandlerData failed!");
1497 coap_delete_pdu(pdu);
1501 cadata->errorInfo->result = result;
1503 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1505 #ifdef WITH_PROCESS_EVENT
1508 oc_event_signal(g_processEvent);
1511 coap_delete_pdu(pdu);
1516 OIC_LOG(DEBUG, TAG, "CAErrorHandler OUT");
1520 static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info, CAResult_t result)
1522 OIC_LOG(DEBUG, TAG, "CASendErrorInfo IN");
1523 #ifndef SINGLE_THREAD
1524 CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
1527 OIC_LOG(ERROR, TAG, "cadata memory allocation failed");
1531 CAEndpoint_t* ep = CACloneEndpoint(endpoint);
1534 OIC_LOG(ERROR, TAG, "endpoint clone failed");
1539 CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
1542 OIC_LOG(ERROR, TAG, "errorInfo memory allocation failed");
1548 CAResult_t res = CACloneInfo(info, &errorInfo->info);
1549 if (CA_STATUS_OK != res)
1551 OIC_LOG(ERROR, TAG, "info clone failed");
1558 errorInfo->result = result;
1559 cadata->remoteEndpoint = ep;
1560 cadata->errorInfo = errorInfo;
1561 cadata->dataType = CA_ERROR_DATA;
1563 CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1565 #ifdef WITH_PROCESS_EVENT
1568 oc_event_signal(g_processEvent);
1570 #endif//WITH_PROCESS_EVENT
1572 OIC_LOG(DEBUG, TAG, "CASendErrorInfo OUT");
1579 static void CALogPDUInfo(const CAData_t *data, const coap_pdu_t *pdu)
1582 if(data == NULL || pdu == NULL)
1584 printf("INVALID INPUT, CALogPDUInfo FAIL\n");
1589 switch(data->dataType)
1591 case CA_REQUEST_DATA:
1592 strncpy(type, "\e[32mREQUEST <<<<\e[m", 30);
1594 case CA_RESPONSE_DATA:
1595 strncpy(type, "\e[36mRESPONSE >>>>\e[m", 30);
1598 strncpy(type, "ERROR", 30);
1600 case CA_RESPONSE_FOR_RES:
1601 strncpy(type, "RESP_RES >>>>", 30);
1604 snprintf(type, 30, "Type : %d", data->dataType);
1609 char method[20] = "";
1610 const CAInfo_t *info = NULL;
1611 if (NULL != data->requestInfo)
1613 switch(data->requestInfo->method)
1616 strncpy(method, "GET", 20);
1619 strncpy(method, "POST", 20);
1622 strncpy(method, "PUT", 20);
1625 strncpy(method, "DEL", 20);
1628 sprintf(method, "Method : %d", data->requestInfo->method);
1631 info = &data->requestInfo->info;
1634 if(NULL != data->responseInfo)
1637 sprintf(method, "result : %d", data->responseInfo->result);
1638 info = &data->responseInfo->info;
1642 char log_buffer[1024] = "";
1643 sprintf(log_buffer, "CA_LOG [%5d] | %-13s | %-12s | msg size : %4d | %s", pdu->transport_hdr->udp.id , type, method, pdu->length, info->resourceUri);
1647 sprintf(log_buffer, "CA_LOG [%5d] | %-13s | %-12s | msg size : %4d | %s", pdu->transport_hdr->udp.id , type, method, pdu->length, info->resourceUri);
1656 static void CALogPDUInfo(const CAData_t *data, const coap_pdu_t *pdu)
1658 OIC_LOG(DEBUG, TAG, "CALogPDUInfo");
1660 VERIFY_NON_NULL_VOID(data, TAG, "data");
1661 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
1662 OIC_TRACE_BEGIN(%s:CALogPDUInfo, TAG);
1664 OIC_LOG(INFO, ANALYZER_TAG, "=================================================");
1665 if(SEND_TYPE_MULTICAST == data->type)
1667 OIC_LOG(INFO, ANALYZER_TAG, "Is Multicast = true");
1671 OIC_LOG(INFO, ANALYZER_TAG, "Is Multicast = false");
1674 if (NULL != data->remoteEndpoint)
1676 CALogAdapterTypeInfo(data->remoteEndpoint->adapter);
1677 OIC_LOG_V(DEBUG, ANALYZER_TAG, "Address = [%s]:[%d]", data->remoteEndpoint->addr,
1678 data->remoteEndpoint->port);
1681 switch(data->dataType)
1683 case CA_REQUEST_DATA:
1684 OIC_LOG(INFO, ANALYZER_TAG, "Data Type = [CA_REQUEST_DATA]");
1686 case CA_RESPONSE_DATA:
1687 OIC_LOG(INFO, ANALYZER_TAG, "Data Type = [CA_RESPONSE_DATA]");
1690 OIC_LOG(INFO, ANALYZER_TAG, "Data Type = [CA_ERROR_DATA]");
1692 case CA_RESPONSE_FOR_RES:
1693 OIC_LOG(INFO, ANALYZER_TAG, "Data Type = [CA_RESPONSE_FOR_RES]");
1696 OIC_LOG_V(INFO, ANALYZER_TAG, "Data Type = [%d]", data->dataType);
1700 const CAInfo_t *info = NULL;
1701 if (NULL != data->requestInfo)
1703 switch(data->requestInfo->method)
1706 OIC_LOG(INFO, ANALYZER_TAG, "Method = [GET]");
1709 OIC_LOG(INFO, ANALYZER_TAG, "Method = [POST]");
1712 OIC_LOG(INFO, ANALYZER_TAG, "Method = [PUT]");
1715 OIC_LOG(INFO, ANALYZER_TAG, "Method = [DELETE]");
1718 OIC_LOG_V(INFO, ANALYZER_TAG, "Method = [%d]", data->requestInfo->method);
1721 info = &data->requestInfo->info;
1724 if (NULL != data->responseInfo)
1726 OIC_LOG_V(INFO, ANALYZER_TAG, "result code = [%d]", data->responseInfo->result);
1727 info = &data->responseInfo->info;
1730 if (pdu->transport_hdr)
1732 OIC_LOG_V(INFO, ANALYZER_TAG, "Msg ID = [%d]", pdu->transport_hdr->udp.id);
1737 OIC_LOG(INFO, ANALYZER_TAG, "Coap Token");
1738 OIC_LOG_BUFFER(INFO, ANALYZER_TAG, (const uint8_t *) info->token, info->tokenLength);
1739 OIC_TRACE_BUFFER("OIC_CA_MSG_HANDLE:CALogPDUInfo:token",
1740 (const uint8_t *) info->token, info->tokenLength);
1741 OIC_LOG_V(INFO_PRIVATE, ANALYZER_TAG, "Res URI = [%s]", info->resourceUri);
1742 OIC_TRACE_MARK(%s:CALogPDUInfo:uri:%s, TAG, info->resourceUri);
1744 if (CA_FORMAT_APPLICATION_CBOR == info->payloadFormat)
1746 OIC_LOG(INFO, ANALYZER_TAG, "Payload Format = [CA_FORMAT_APPLICATION_CBOR]");
1750 OIC_LOG_V(INFO, ANALYZER_TAG, "Payload Format = [%d]", info->payloadFormat);
1754 size_t payloadLen = (pdu->data) ? (unsigned char *) pdu->hdr + pdu->length - pdu->data : 0;
1755 OIC_LOG_V(INFO, ANALYZER_TAG, "CoAP Message Full Size = [%u]", pdu->length);
1756 OIC_LOG(INFO, ANALYZER_TAG, "CoAP Header (+ 0xFF)");
1757 OIC_LOG_BUFFER(INFO, ANALYZER_TAG, (const uint8_t *) pdu->transport_hdr,
1758 pdu->length - payloadLen);
1759 OIC_LOG_V(INFO, ANALYZER_TAG, "CoAP Header size = [%" PRIuPTR "]", (size_t) pdu->length - payloadLen);
1761 OIC_LOG_V(INFO, ANALYZER_TAG, "CoAP Payload");
1762 OIC_LOG_BUFFER(INFO_PRIVATE, ANALYZER_TAG, pdu->data, payloadLen);
1763 OIC_LOG_V(INFO, ANALYZER_TAG, "CoAP Payload Size = [%" PRIuPTR "]", payloadLen);
1764 OIC_LOG(INFO, ANALYZER_TAG, "=================================================");
1767 CASamsungLogMessage(data, pdu);
1771 static void CASamsungLogMessage(const CAData_t *data, const coap_pdu_t *pdu)
1773 OIC_LOG(INFO, TAG, "CASamsungLogMessage");
1774 VERIFY_NON_NULL_VOID(data, TAG, "data");
1775 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
1776 VERIFY_NON_NULL_VOID(data->remoteEndpoint, TAG, "data->remoteEndpoint");
1778 const CAInfo_t *info = NULL;
1779 if (NULL != data->requestInfo)
1781 info = &data->requestInfo->info;
1784 if (NULL != data->responseInfo)
1786 info = &data->responseInfo->info;
1789 VERIFY_NON_NULL_VOID(info, TAG, "info");
1791 memset(g_headerBuffer, 0, MAX_LOG_BUFFER_SIZE);
1794 g_headerBuffer[g_headerIndex++] = data->dataType;
1795 g_headerBuffer[g_headerIndex++] = '|';
1796 g_headerBuffer[g_headerIndex++] = data->remoteEndpoint->adapter;
1797 g_headerBuffer[g_headerIndex++] = '|';
1798 g_headerBuffer[g_headerIndex++] = data->type;
1799 g_headerBuffer[g_headerIndex++] = '|';
1801 if (NULL != data->remoteEndpoint)
1804 while (data->remoteEndpoint->addr[i])
1806 g_headerBuffer[g_headerIndex++] = data->remoteEndpoint->addr[i];
1809 g_headerBuffer[g_headerIndex++] = ':';
1810 g_headerBuffer[g_headerIndex++] = (data->remoteEndpoint->port >> 8) & 0x0000ff;
1811 g_headerBuffer[g_headerIndex++] = data->remoteEndpoint->port & 0x000000ff;
1814 g_headerBuffer[g_headerIndex++] = '|';
1815 if (data->requestInfo)
1817 g_headerBuffer[g_headerIndex++] = data->requestInfo->method;
1821 g_headerBuffer[g_headerIndex++] = 0;
1824 g_headerBuffer[g_headerIndex++] = '|';
1825 if (data->responseInfo)
1827 g_headerBuffer[g_headerIndex++] = data->responseInfo->result;
1831 g_headerBuffer[g_headerIndex++] = 0;
1833 g_headerBuffer[g_headerIndex++] = '|';
1835 if (pdu->transport_hdr)
1837 g_headerBuffer[g_headerIndex++] = (pdu->transport_hdr->udp.id >> 8) & 0x0000ff;
1838 g_headerBuffer[g_headerIndex++] = pdu->transport_hdr->udp.id & 0x000000ff;
1842 g_headerBuffer[g_headerIndex++] = 0;
1843 g_headerBuffer[g_headerIndex++] = 0;
1845 g_headerBuffer[g_headerIndex++] = '|';
1847 if (info->token && info->tokenLength > 0)
1849 for (size_t i = 0; i < info->tokenLength; i++)
1851 g_headerBuffer[g_headerIndex++] = info->token[i];
1853 g_headerBuffer[g_headerIndex++] = '|';
1856 if (info->resourceUri)
1859 while (info->resourceUri[i])
1861 g_headerBuffer[g_headerIndex++] = info->resourceUri[i];
1864 g_headerBuffer[g_headerIndex++] = '|';
1867 OIC_LOG_CA_BUFFER(INFO, TAG, (uint8_t *) g_headerBuffer, g_headerIndex, 1);
1868 size_t payloadLen = (unsigned char *) pdu->hdr + pdu->length - pdu->data;
1869 OIC_LOG_CA_BUFFER(INFO_PRIVATE, TAG, pdu->data, payloadLen, 0);
1874 static void CALogPDUInfo(const CAData_t *data, const coap_pdu_t *pdu)
1876 VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
1879 OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
1880 OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->transport_hdr->udp.type);
1881 OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->transport_hdr->udp.code);
1882 OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
1883 OIC_LOG_BUFFER(DEBUG, TAG, pdu->transport_hdr->udp.token,
1884 pdu->transport_hdr->udp.token_length);
1888 #ifdef WITH_PROCESS_EVENT
1889 void CARegisterMessageProcessEvent(oc_event event)
1891 g_processEvent = event;
1893 #endif // WITH_PROCESS_EVENT