add checking logic of pdu data length.
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / camessagehandler.c
1 /* *****************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdint.h>
25
26 #include "cainterface.h"
27 #include "camessagehandler.h"
28 #include "caremotehandler.h"
29 #include "caprotocolmessage.h"
30 #include "logger.h"
31 #ifndef WITH_UPSTREAM_LIBCOAP
32 #include "coap/config.h"
33 #endif
34 #include "oic_malloc.h"
35 #include "canetworkconfigurator.h"
36 #include "caadapterutils.h"
37 #include "cainterfacecontroller.h"
38 #include "caretransmission.h"
39 #include "oic_string.h"
40
41 #ifdef WITH_BWT
42 #include "cablockwisetransfer.h"
43 #endif
44
45 #ifndef  SINGLE_THREAD
46 #include "uqueue.h"
47 #include "cathreadpool.h" /* for thread pool */
48 #include "caqueueingthread.h"
49
50 #define SINGLE_HANDLE
51 #define MAX_THREAD_POOL_SIZE    20
52
53 // thread pool handle
54 static ca_thread_pool_t g_threadPoolHandle = NULL;
55
56 // message handler main thread
57 static CAQueueingThread_t g_sendThread;
58 static CAQueueingThread_t g_receiveThread;
59
60 #else
61 #define CA_MAX_RT_ARRAY_SIZE    3
62 #endif  // SINGLE_THREAD
63
64 #define TAG "OIC_CA_MSG_HANDLE"
65
66 static CARetransmission_t g_retransmissionContext;
67
68 // handler field
69 static CARequestCallback g_requestHandler = NULL;
70 static CAResponseCallback g_responseHandler = NULL;
71 static CAErrorCallback g_errorHandler = NULL;
72 static CANetworkMonitorCallback g_nwMonitorHandler = NULL;
73
74 static void CAErrorHandler(const CAEndpoint_t *endpoint,
75                            const void *data, size_t dataLen,
76                            CAResult_t result);
77
78 static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint,
79                                        const CARemoteId_t *identity,
80                                        const void *data, CADataType_t dataType);
81
82 static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info,
83                             CAResult_t result);
84
85 #ifdef SINGLE_THREAD
86 static void CAProcessReceivedData(CAData_t *data);
87 #endif
88 static void CADestroyData(void *data, uint32_t size);
89 static void CALogPayloadInfo(CAInfo_t *info);
90 static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *endpoint, uint16_t id,
91                                 CAToken_t token, uint8_t tokenLength);
92
93 /**
94  * print send / receive message of CoAP.
95  * @param[in] data      CA information which has send/receive message and endpoint.
96  * @param[in] pdu       CoAP pdu low data.
97  */
98 static void CALogPDUInfo(const CAData_t *data, const coap_pdu_t *pdu);
99
100 #ifdef WITH_BWT
101 void CAAddDataToSendThread(CAData_t *data)
102 {
103     VERIFY_NON_NULL_VOID(data, TAG, "data");
104
105     // add thread
106     CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
107 }
108
109 void CAAddDataToReceiveThread(CAData_t *data)
110 {
111     VERIFY_NON_NULL_VOID(data, TAG, "data");
112
113     // add thread
114     CAQueueingThreadAddData(&g_receiveThread, data, sizeof(CAData_t));
115 }
116 #endif
117
118 static bool CAIsSelectedNetworkAvailable()
119 {
120     u_arraylist_t *list = CAGetSelectedNetworkList();
121     if (!list || u_arraylist_length(list) == 0)
122     {
123         OIC_LOG(ERROR, TAG, "No selected network");
124         return false;
125     }
126
127     return true;
128 }
129
130 static CAData_t* CAGenerateHandlerData(const CAEndpoint_t *endpoint,
131                                        const CARemoteId_t *identity,
132                                        const void *data, CADataType_t dataType)
133 {
134     OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData IN");
135     CAInfo_t *info = NULL;
136     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
137     if (!cadata)
138     {
139         OIC_LOG(ERROR, TAG, "memory allocation failed");
140         return NULL;
141     }
142 #ifdef SINGLE_THREAD
143     CAEndpoint_t* ep = endpoint;
144 #else
145     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
146     if (!ep)
147     {
148         OIC_LOG(ERROR, TAG, "endpoint clone failed");
149         goto exit;
150     }
151 #endif
152
153     OIC_LOG_V(DEBUG, TAG, "address : %s", ep->addr);
154
155     if (CA_RESPONSE_DATA == dataType)
156     {
157         CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
158         if (!resInfo)
159         {
160             OIC_LOG(ERROR, TAG, "memory allocation failed");
161             goto exit;
162         }
163
164         CAResult_t result = CAGetResponseInfoFromPDU(data, resInfo, endpoint);
165         if (CA_STATUS_OK != result)
166         {
167             OIC_LOG(ERROR, TAG, "CAGetResponseInfoFromPDU Failed");
168             CADestroyResponseInfoInternal(resInfo);
169             goto exit;
170         }
171         cadata->responseInfo = resInfo;
172         info = &resInfo->info;
173         if (identity)
174         {
175             info->identity = *identity;
176         }
177         OIC_LOG(DEBUG, TAG, "Response Info :");
178         CALogPayloadInfo(info);
179     }
180     else if (CA_REQUEST_DATA == dataType)
181     {
182         CARequestInfo_t* reqInfo = (CARequestInfo_t*)OICCalloc(1, sizeof(CARequestInfo_t));
183         if (!reqInfo)
184         {
185             OIC_LOG(ERROR, TAG, "memory allocation failed");
186             goto exit;
187         }
188
189         CAResult_t result = CAGetRequestInfoFromPDU(data, endpoint, reqInfo);
190         if (CA_STATUS_OK != result)
191         {
192             OIC_LOG(ERROR, TAG, "CAGetRequestInfoFromPDU failed");
193             CADestroyRequestInfoInternal(reqInfo);
194             goto exit;
195         }
196
197         if (CADropSecondMessage(&caglobals.ca.requestHistory, endpoint, reqInfo->info.messageId,
198                                 reqInfo->info.token, reqInfo->info.tokenLength))
199         {
200             OIC_LOG(INFO, TAG, "Second Request with same Token, Drop it");
201             CADestroyRequestInfoInternal(reqInfo);
202             goto exit;
203         }
204
205         cadata->requestInfo = reqInfo;
206         info = &reqInfo->info;
207         if (identity)
208         {
209             info->identity = *identity;
210         }
211         OIC_LOG(DEBUG, TAG, "Request Info :");
212         CALogPayloadInfo(info);
213    }
214     else if (CA_ERROR_DATA == dataType)
215     {
216         CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
217         if (!errorInfo)
218         {
219             OIC_LOG(ERROR, TAG, "Memory allocation failed!");
220             goto exit;
221         }
222
223         CAResult_t result = CAGetErrorInfoFromPDU(data, endpoint, errorInfo);
224         if (CA_STATUS_OK != result)
225         {
226             OIC_LOG(ERROR, TAG, "CAGetErrorInfoFromPDU failed");
227             OICFree(errorInfo);
228             goto exit;
229         }
230
231         cadata->errorInfo = errorInfo;
232         info = &errorInfo->info;
233         if (identity)
234         {
235             info->identity = *identity;
236         }
237         OIC_LOG(DEBUG, TAG, "error Info :");
238         CALogPayloadInfo(info);
239     }
240
241     cadata->remoteEndpoint = ep;
242     cadata->dataType = dataType;
243
244     OIC_LOG(DEBUG, TAG, "CAGenerateHandlerData OUT");
245     return cadata;
246
247 exit:
248     OICFree(cadata);
249 #ifndef SINGLE_THREAD
250     CAFreeEndpoint(ep);
251 #endif
252     return NULL;
253 }
254
255 static void CATimeoutCallback(const CAEndpoint_t *endpoint, const void *pdu, uint32_t size)
256 {
257     VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
258     VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
259 #ifdef SINGLE_THREAD
260     CAEndpoint_t* ep = endpoint;
261 #else
262     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
263     if (!ep)
264     {
265         OIC_LOG(ERROR, TAG, "clone failed");
266         return;
267     }
268 #endif
269
270     CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
271
272     if (!resInfo)
273     {
274         OIC_LOG(ERROR, TAG, "calloc failed");
275 #ifndef SINGLE_THREAD
276         CAFreeEndpoint(ep);
277 #endif
278         return;
279     }
280
281     resInfo->result = CA_RETRANSMIT_TIMEOUT;
282     resInfo->info.type = CAGetMessageTypeFromPduBinaryData(pdu, size);
283     resInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
284
285     CAResult_t res = CAGetTokenFromPDU((const coap_hdr_transport_t *) pdu, &(resInfo->info),
286                                        endpoint);
287     if (CA_STATUS_OK != res)
288     {
289         OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
290         CADestroyResponseInfoInternal(resInfo);
291 #ifndef SINGLE_THREAD
292         CAFreeEndpoint(ep);
293 #endif
294         return;
295     }
296
297     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
298     if (NULL == cadata)
299     {
300         OIC_LOG(ERROR, TAG, "memory allocation failed !");
301 #ifndef SINGLE_THREAD
302         CAFreeEndpoint(ep);
303 #endif
304         CADestroyResponseInfoInternal(resInfo);
305         return;
306     }
307
308     cadata->type = SEND_TYPE_UNICAST;
309     cadata->remoteEndpoint = ep;
310     cadata->requestInfo = NULL;
311     cadata->responseInfo = resInfo;
312
313 #ifdef WITH_BWT
314     if (CAIsSupportedBlockwiseTransfer(endpoint->adapter))
315     {
316         res = CARemoveBlockDataFromListWithSeed(resInfo->info.token, resInfo->info.tokenLength,
317                                                 endpoint->addr, endpoint->port);
318         if (CA_STATUS_OK != res)
319         {
320             OIC_LOG(ERROR, TAG, "CARemoveBlockDataFromListWithSeed failed");
321         }
322     }
323 #endif // WITH_BWT
324
325 #ifdef SINGLE_THREAD
326     CAProcessReceivedData(cadata);
327 #else
328     CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
329 #endif
330 }
331
332 static void CADestroyData(void *data, uint32_t size)
333 {
334     OIC_LOG(DEBUG, TAG, "CADestroyData IN");
335     if ((size_t)size < sizeof(CAData_t))
336     {
337         OIC_LOG_V(ERROR, TAG, "Destroy data too small %p %d", data, size);
338     }
339     CAData_t *cadata = (CAData_t *) data;
340
341     if (NULL == cadata)
342     {
343         OIC_LOG(ERROR, TAG, "cadata is NULL");
344         return;
345     }
346 #ifndef SINGLE_THREAD
347     if (NULL != cadata->remoteEndpoint)
348     {
349         CAFreeEndpoint(cadata->remoteEndpoint);
350     }
351 #endif
352
353     if (NULL != cadata->requestInfo)
354     {
355         CADestroyRequestInfoInternal((CARequestInfo_t *) cadata->requestInfo);
356     }
357
358     if (NULL != cadata->responseInfo)
359     {
360         CADestroyResponseInfoInternal((CAResponseInfo_t *) cadata->responseInfo);
361     }
362
363     if (NULL != cadata->errorInfo)
364     {
365         CADestroyErrorInfoInternal(cadata->errorInfo);
366     }
367
368     OICFree(cadata);
369     OIC_LOG(DEBUG, TAG, "CADestroyData OUT");
370 }
371
372 #ifdef SINGLE_THREAD
373 static void CAProcessReceivedData(CAData_t *data)
374 {
375     OIC_LOG(DEBUG, TAG, "CAProcessReceivedData IN");
376     if (!data)
377     {
378         OIC_LOG(ERROR, TAG, "thread data error!!");
379         return;
380     }
381
382     // parse the data and call the callbacks.
383     // #1 parse the data
384     // #2 get endpoint
385     CAEndpoint_t *rep = (CAEndpoint_t *)(data->remoteEndpoint);
386     if (!rep)
387     {
388         OIC_LOG(ERROR, TAG, "remoteEndpoint error!!");
389         return;
390     }
391
392     if (data->requestInfo && g_requestHandler)
393     {
394         g_requestHandler(rep, data->requestInfo);
395     }
396     else if (data->responseInfo && g_responseHandler)
397     {
398         g_responseHandler(rep, data->responseInfo);
399     }
400     else if (data->errorInfo && g_errorHandler)
401     {
402         g_errorHandler(rep, data->errorInfo);
403     }
404
405     CADestroyData(data, sizeof(CAData_t));
406
407     OIC_LOG(DEBUG, TAG, "CAProcessReceivedData OUT");
408 }
409 #endif
410
411 #ifndef SINGLE_THREAD
412 static void CAReceiveThreadProcess(void *threadData)
413 {
414 #ifndef SINGLE_HANDLE
415     CAData_t *data = (CAData_t *) threadData;
416     CAProcessReceivedData(data);
417 #else
418     (void)threadData;
419 #endif
420 }
421 #endif // SINGLE_THREAD
422
423 static CAResult_t CAProcessMulticastData(const CAData_t *data)
424 {
425     VERIFY_NON_NULL(data, TAG, "data");
426     VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");
427
428     coap_pdu_t *pdu = NULL;
429     CAInfo_t *info = NULL;
430     coap_list_t *options = NULL;
431     coap_transport_t transport = COAP_UDP;
432     CAResult_t res = CA_SEND_FAILED;
433
434     if (!data->requestInfo && !data->responseInfo)
435     {
436         OIC_LOG(ERROR, TAG, "request or response info is empty");
437         return res;
438     }
439
440     if (data->requestInfo)
441     {
442         OIC_LOG(DEBUG, TAG, "requestInfo is available..");
443
444         info = &data->requestInfo->info;
445         pdu = CAGeneratePDU(CA_GET, info, data->remoteEndpoint, &options, &transport);
446     }
447     else if (data->responseInfo)
448     {
449         OIC_LOG(DEBUG, TAG, "responseInfo is available..");
450
451         info = &data->responseInfo->info;
452         pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint,
453                             &options, &transport);
454     }
455
456     if (!pdu)
457     {
458         OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
459         CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
460         coap_delete_list(options);
461         return res;
462     }
463
464 #ifdef WITH_BWT
465     if (CAIsSupportedBlockwiseTransfer(data->remoteEndpoint->adapter))
466     {
467         // Blockwise transfer
468         res = CAAddBlockOption(&pdu, info, data->remoteEndpoint, &options);
469         if (CA_STATUS_OK != res)
470         {
471             OIC_LOG(DEBUG, TAG, "CAAddBlockOption has failed");
472             goto exit;
473         }
474     }
475 #endif // WITH_BWT
476
477     CALogPDUInfo(data, pdu);
478
479     res = CASendMulticastData(data->remoteEndpoint, pdu->transport_hdr, pdu->length, data->dataType);
480     if (CA_STATUS_OK != res)
481     {
482         OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
483         goto exit;
484     }
485
486     coap_delete_list(options);
487     coap_delete_pdu(pdu);
488     return res;
489
490 exit:
491     CAErrorHandler(data->remoteEndpoint, pdu->transport_hdr, pdu->length, res);
492     coap_delete_list(options);
493     coap_delete_pdu(pdu);
494     return res;
495 }
496
497 static CAResult_t CAProcessSendData(const CAData_t *data)
498 {
499     VERIFY_NON_NULL(data, TAG, "data");
500     VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");
501
502     CAResult_t res = CA_STATUS_FAILED;
503
504     CASendDataType_t type = data->type;
505
506     coap_pdu_t *pdu = NULL;
507     CAInfo_t *info = NULL;
508     coap_list_t *options = NULL;
509     coap_transport_t transport = COAP_UDP;
510
511     if (SEND_TYPE_UNICAST == type)
512     {
513         OIC_LOG(DEBUG,TAG,"Unicast message");
514
515 #ifdef ROUTING_GATEWAY
516         /*
517          * When forwarding a packet, do not attempt retransmission as its the responsibility of
518          * packet originator node
519          */
520         bool skipRetransmission = false;
521 #endif
522
523         if (NULL != data->requestInfo)
524         {
525             OIC_LOG(DEBUG, TAG, "requestInfo is available..");
526
527             info = &data->requestInfo->info;
528 #ifdef ROUTING_GATEWAY
529             skipRetransmission = data->requestInfo->info.skipRetransmission;
530 #endif
531             pdu = CAGeneratePDU(data->requestInfo->method, info, data->remoteEndpoint,
532                                 &options, &transport);
533         }
534         else if (NULL != data->responseInfo)
535         {
536             OIC_LOG(DEBUG, TAG, "responseInfo is available..");
537
538             info = &data->responseInfo->info;
539 #ifdef ROUTING_GATEWAY
540             skipRetransmission = data->responseInfo->info.skipRetransmission;
541 #endif
542             pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint,
543                                 &options, &transport);
544         }
545         else
546         {
547             OIC_LOG(DEBUG, TAG, "request info, response info is empty");
548             return CA_STATUS_INVALID_PARAM;
549         }
550
551         // interface controller function call.
552         if (NULL != pdu)
553         {
554 #ifdef WITH_BWT
555             if (CAIsSupportedBlockwiseTransfer(data->remoteEndpoint->adapter))
556             {
557                 // Blockwise transfer
558                 if (NULL != info)
559                 {
560                     CAResult_t res = CAAddBlockOption(&pdu, info,
561                                                       data->remoteEndpoint,
562                                                       &options);
563                     if (CA_STATUS_OK != res)
564                     {
565                         OIC_LOG(INFO, TAG, "to write block option has failed");
566                         CAErrorHandler(data->remoteEndpoint, pdu->transport_hdr, pdu->length, res);
567                         coap_delete_list(options);
568                         coap_delete_pdu(pdu);
569                         return res;
570                     }
571                 }
572             }
573 #endif // WITH_BWT
574             CALogPDUInfo(data, pdu);
575
576             OIC_LOG_V(INFO, TAG, "CASendUnicastData type : %d", data->dataType);
577             res = CASendUnicastData(data->remoteEndpoint, pdu->transport_hdr, pdu->length, data->dataType);
578             if (CA_STATUS_OK != res)
579             {
580                 OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
581                 CAErrorHandler(data->remoteEndpoint, pdu->transport_hdr, pdu->length, res);
582                 coap_delete_list(options);
583                 coap_delete_pdu(pdu);
584                 return res;
585             }
586
587 #ifdef WITH_TCP
588             if (CAIsSupportedCoAPOverTCP(data->remoteEndpoint->adapter))
589             {
590                 OIC_LOG(INFO, TAG, "retransmission will be not worked");
591             }
592             else
593 #endif
594 #ifdef ROUTING_GATEWAY
595             if (!skipRetransmission)
596 #endif
597             {
598                 // for retransmission
599                 res = CARetransmissionSentData(&g_retransmissionContext,
600                                                data->remoteEndpoint,
601                                                data->dataType,
602                                                pdu->transport_hdr, pdu->length);
603                 if ((CA_STATUS_OK != res) && (CA_NOT_SUPPORTED != res))
604                 {
605                     //when retransmission not supported this will return CA_NOT_SUPPORTED, ignore
606                     OIC_LOG_V(INFO, TAG, "retransmission is not enabled due to error, res : %d", res);
607                     coap_delete_list(options);
608                     coap_delete_pdu(pdu);
609                     return res;
610                 }
611             }
612
613             coap_delete_list(options);
614             coap_delete_pdu(pdu);
615         }
616         else
617         {
618             OIC_LOG(ERROR,TAG,"Failed to generate unicast PDU");
619             CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
620             return CA_SEND_FAILED;
621         }
622     }
623     else if (SEND_TYPE_MULTICAST == type)
624     {
625         OIC_LOG(DEBUG,TAG,"Multicast message");
626 #ifdef WITH_TCP
627         /*
628          * If CoAP over TCP is enabled, the CoAP pdu wont be same for IP and other adapters.
629          * That's why we need to generate two pdu's, one for IP and second for other transports.
630          * Two possible cases we might have to split: a) when adapter is CA_DEFAULT_ADAPTER
631          * b) when one of the adapter is IP adapter(ex: CA_ADAPTER_IP | CA_ADAPTER_GATT_BTLE)
632          */
633         if (data->remoteEndpoint->adapter == CA_DEFAULT_ADAPTER ||
634                 (CA_ADAPTER_IP & data->remoteEndpoint->adapter &&
635                     CA_ADAPTER_IP != data->remoteEndpoint->adapter))
636         {
637             if (data->remoteEndpoint->adapter == CA_DEFAULT_ADAPTER)
638             {
639                 data->remoteEndpoint->adapter = CA_ALL_ADAPTERS ^ CA_ADAPTER_IP;
640             }
641             else
642             {
643                 data->remoteEndpoint->adapter = data->remoteEndpoint->adapter ^ CA_ADAPTER_IP;
644             }
645             CAProcessMulticastData(data);
646             data->remoteEndpoint->adapter = CA_ADAPTER_IP;
647             CAProcessMulticastData(data);
648         }
649         else
650         {
651             CAProcessMulticastData(data);
652         }
653 #else
654         CAProcessMulticastData(data);
655 #endif
656     }
657
658     return CA_STATUS_OK;
659 }
660
661 #ifndef SINGLE_THREAD
662 static void CASendThreadProcess(void *threadData)
663 {
664     CAData_t *data = (CAData_t *) threadData;
665     CAProcessSendData(data);
666 }
667 #endif
668
669 /*
670  * If a second message arrives with the same message ID, token and the other address
671  * family, drop it.  Typically, IPv6 beats IPv4, so the IPv4 message is dropped.
672  */
673 static bool CADropSecondMessage(CAHistory_t *history, const CAEndpoint_t *ep, uint16_t id,
674                                 CAToken_t token, uint8_t tokenLength)
675 {
676     if (!ep)
677     {
678         return true;
679     }
680     if (ep->adapter != CA_ADAPTER_IP)
681     {
682         return false;
683     }
684     if (!caglobals.ip.dualstack)
685     {
686         return false;
687     }
688
689     if (tokenLength > CA_MAX_TOKEN_LEN)
690     {
691         /*
692          * If token length is more than CA_MAX_TOKEN_LEN,
693          * we compare the first CA_MAX_TOKEN_LEN bytes only.
694          */
695         tokenLength = CA_MAX_TOKEN_LEN;
696     }
697
698     bool ret = false;
699     CATransportFlags_t familyFlags = ep->flags & CA_IPFAMILY_MASK;
700
701     for (size_t i = 0; i < sizeof(history->items) / sizeof(history->items[0]); i++)
702     {
703         CAHistoryItem_t *item = &(history->items[i]);
704         if (id == item->messageId && tokenLength == item->tokenLength
705             && ep->ifindex == item->ifindex && memcmp(item->token, token, tokenLength) == 0)
706         {
707             if ((familyFlags ^ item->flags) == CA_IPFAMILY_MASK)
708             {
709                 OIC_LOG_V(INFO, TAG, "IPv%c duplicate message ignored",
710                           familyFlags & CA_IPV6 ? '6' : '4');
711                 ret = true;
712                 break;
713             }
714         }
715     }
716
717     history->items[history->nextIndex].flags = familyFlags;
718     history->items[history->nextIndex].messageId = id;
719     history->items[history->nextIndex].ifindex = ep->ifindex;
720     if (token && tokenLength)
721     {
722         memcpy(history->items[history->nextIndex].token, token, tokenLength);
723         history->items[history->nextIndex].tokenLength = tokenLength;
724     }
725
726     if (++history->nextIndex >= HISTORYSIZE)
727     {
728         history->nextIndex = 0;
729     }
730
731     return ret;
732 }
733
734 static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
735                                      const void *data, size_t dataLen)
736 {
737     VERIFY_NON_NULL_VOID(sep, TAG, "remoteEndpoint");
738     VERIFY_NON_NULL_VOID(data, TAG, "data");
739
740     if (0 == dataLen)
741     {
742         OIC_LOG(ERROR, TAG, "dataLen is zero");
743         return;
744     }
745
746     uint32_t code = CA_NOT_FOUND;
747     CAData_t *cadata = NULL;
748
749     coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code,
750                                                 &(sep->endpoint));
751     if (NULL == pdu)
752     {
753         OIC_LOG(ERROR, TAG, "Parse PDU failed");
754         goto exit;
755     }
756
757     OIC_LOG_V(DEBUG, TAG, "code = %d", code);
758     if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
759     {
760         cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_REQUEST_DATA);
761         if (!cadata)
762         {
763             OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
764             coap_delete_pdu(pdu);
765             goto exit;
766         }
767     }
768     else
769     {
770         cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_RESPONSE_DATA);
771         if (!cadata)
772         {
773             OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
774             coap_delete_pdu(pdu);
775             goto exit;
776         }
777
778 #ifdef WITH_TCP
779         if (CAIsSupportedCoAPOverTCP(sep->endpoint.adapter))
780         {
781             OIC_LOG(INFO, TAG, "retransmission is not supported");
782         }
783         else
784 #endif
785         {
786             // for retransmission
787             void *retransmissionPdu = NULL;
788             CARetransmissionReceivedData(&g_retransmissionContext, cadata->remoteEndpoint, pdu->transport_hdr,
789                                          pdu->length, &retransmissionPdu);
790
791             // get token from saved data in retransmission list
792             if (retransmissionPdu && CA_EMPTY == code)
793             {
794                 if (cadata->responseInfo)
795                 {
796                     CAInfo_t *info = &cadata->responseInfo->info;
797                     CAResult_t res = CAGetTokenFromPDU((const coap_hdr_transport_t *)retransmissionPdu,
798                                                        info, &(sep->endpoint));
799                     if (CA_STATUS_OK != res)
800                     {
801                         OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
802                         OICFree(info->token);
803                         info->tokenLength = 0;
804                     }
805                 }
806             }
807             OICFree(retransmissionPdu);
808         }
809     }
810
811     cadata->type = SEND_TYPE_UNICAST;
812
813     CALogPDUInfo(cadata, pdu);
814
815 #ifdef SINGLE_THREAD
816     CAProcessReceivedData(cadata);
817 #else
818 #ifdef WITH_BWT
819     if (CAIsSupportedBlockwiseTransfer(sep->endpoint.adapter))
820     {
821         CAResult_t res = CAReceiveBlockWiseData(pdu, &(sep->endpoint), cadata, dataLen);
822         if (CA_NOT_SUPPORTED == res || CA_REQUEST_TIMEOUT == res)
823         {
824             OIC_LOG(DEBUG, TAG, "this message does not have block option");
825             CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
826         }
827         else
828         {
829             CADestroyData(cadata, sizeof(CAData_t));
830         }
831     }
832     else
833 #endif
834     {
835         CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
836     }
837 #endif // SINGLE_THREAD
838
839     coap_delete_pdu(pdu);
840
841 exit:
842     OIC_LOG(DEBUG, TAG, "received pdu data :");
843     OIC_LOG_BUFFER(DEBUG, TAG,  data, dataLen);
844 }
845
846 void CAHandleRequestResponseCallbacks()
847 {
848 #ifdef SINGLE_THREAD
849     CAReadData();
850     CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
851 #else
852 #ifdef SINGLE_HANDLE
853     // parse the data and call the callbacks.
854     // #1 parse the data
855     // #2 get endpoint
856
857     oc_mutex_lock(g_receiveThread.threadMutex);
858
859     u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
860
861     oc_mutex_unlock(g_receiveThread.threadMutex);
862
863     if (NULL == item || NULL == item->msg)
864     {
865         return;
866     }
867
868     // get endpoint
869     CAData_t *td = (CAData_t *) item->msg;
870
871     if (td->requestInfo && g_requestHandler)
872     {
873         OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
874         g_requestHandler(td->remoteEndpoint, td->requestInfo);
875     }
876     else if (td->responseInfo && g_responseHandler)
877     {
878         OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
879         g_responseHandler(td->remoteEndpoint, td->responseInfo);
880     }
881     else if (td->errorInfo && g_errorHandler)
882     {
883         OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
884         g_errorHandler(td->remoteEndpoint, td->errorInfo);
885     }
886
887     CADestroyData(item->msg, sizeof(CAData_t));
888     OICFree(item);
889
890 #endif // SINGLE_HANDLE
891 #endif // SINGLE_THREAD
892 }
893
894 static CAData_t* CAPrepareSendData(const CAEndpoint_t *endpoint, const void *sendData,
895                                    CADataType_t dataType)
896 {
897     OIC_LOG(DEBUG, TAG, "CAPrepareSendData IN");
898
899     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
900     if (!cadata)
901     {
902         OIC_LOG(ERROR, TAG, "memory allocation failed");
903         return NULL;
904     }
905
906     if (CA_REQUEST_DATA == dataType)
907     {
908 #ifdef SINGLE_THREAD
909         CARequestInfo_t *request = (CARequestInfo_t *)sendData;
910 #else
911         // clone request info
912         CARequestInfo_t *request = CACloneRequestInfo((CARequestInfo_t *)sendData);
913         if (!request)
914         {
915             OIC_LOG(ERROR, TAG, "CACloneRequestInfo failed");
916             goto exit;
917         }
918 #endif
919         cadata->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
920         cadata->requestInfo =  request;
921     }
922     else if (CA_RESPONSE_DATA == dataType || CA_RESPONSE_FOR_RES == dataType)
923     {
924 #ifdef SINGLE_THREAD
925         CAResponseInfo_t *response = (CAResponseInfo_t *)sendData;
926 #else
927         // clone response info
928         CAResponseInfo_t *response = CACloneResponseInfo((CAResponseInfo_t *)sendData);
929         if(!response)
930         {
931             OIC_LOG(ERROR, TAG, "CACloneResponseInfo failed");
932             goto exit;
933         }
934 #endif
935         cadata->type = response->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
936         cadata->responseInfo = response;
937     }
938     else
939     {
940         OIC_LOG(ERROR, TAG, "CAPrepareSendData unknown data type");
941         goto exit;
942     }
943
944 #ifdef SINGLE_THREAD
945     CAEndpoint_t* ep = endpoint;
946 #else
947     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
948     if (!ep)
949     {
950         OIC_LOG(ERROR, TAG, "endpoint clone failed");
951         goto exit;
952     }
953 #endif
954     cadata->remoteEndpoint = ep;
955     cadata->dataType = dataType;
956     return cadata;
957
958 exit:
959 #ifndef SINGLE_THREAD
960     CADestroyData(cadata, sizeof(CAData_t));
961 #else
962     OICFree(cadata);
963 #endif
964     return NULL;
965 }
966
967 CAResult_t CADetachSendMessage(const CAEndpoint_t *endpoint, const void *sendMsg,
968                                CADataType_t dataType)
969 {
970     VERIFY_NON_NULL(endpoint, TAG, "endpoint");
971     VERIFY_NON_NULL(sendMsg, TAG, "sendMsg");
972
973     if (false == CAIsSelectedNetworkAvailable())
974     {
975         return CA_STATUS_FAILED;
976     }
977
978 #ifdef ARDUINO
979     // If max retransmission queue is reached, then don't handle new request
980     if (CA_MAX_RT_ARRAY_SIZE == u_arraylist_length(g_retransmissionContext.dataList))
981     {
982         OIC_LOG(ERROR, TAG, "max RT queue size reached!");
983         return CA_SEND_FAILED;
984     }
985 #endif // ARDUINO
986
987     CAData_t *data = CAPrepareSendData(endpoint, sendMsg, dataType);
988     if(!data)
989     {
990         OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
991         return CA_MEMORY_ALLOC_FAILED;
992     }
993
994     OIC_LOG_V(DEBUG, TAG, "device ID of endpoint of this message is %s", endpoint->deviceId);
995
996 #ifdef SINGLE_THREAD
997     CAResult_t result = CAProcessSendData(data);
998     if (CA_STATUS_OK != result)
999     {
1000         OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
1001         OICFree(data);
1002         return result;
1003     }
1004
1005     OICFree(data);
1006
1007 #else
1008     if (SEND_TYPE_UNICAST == data->type && CAIsLocalEndpoint(data->remoteEndpoint))
1009     {
1010         OIC_LOG(DEBUG, TAG,
1011                 "This is a loopback message. Transfer it to the receive queue directly");
1012         CAQueueingThreadAddData(&g_receiveThread, data, sizeof(CAData_t));
1013         return CA_STATUS_OK;
1014     }
1015 #ifdef WITH_BWT
1016     if (CAIsSupportedBlockwiseTransfer(endpoint->adapter))
1017     {
1018         // send block data
1019         CAResult_t res = CASendBlockWiseData(data);
1020         if (CA_NOT_SUPPORTED == res)
1021         {
1022             OIC_LOG(DEBUG, TAG, "normal msg will be sent");
1023             CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
1024             return CA_STATUS_OK;
1025         }
1026         else
1027         {
1028             CADestroyData(data, sizeof(CAData_t));
1029         }
1030         return res;
1031     }
1032     else
1033 #endif // WITH_BWT
1034     {
1035         CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
1036     }
1037 #endif // SINGLE_THREAD
1038
1039     return CA_STATUS_OK;
1040 }
1041
1042 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
1043                              CAErrorCallback errorHandler)
1044 {
1045     g_requestHandler = ReqHandler;
1046     g_responseHandler = RespHandler;
1047     g_errorHandler = errorHandler;
1048 }
1049
1050 void CASetNetworkMonitorCallback(CANetworkMonitorCallback nwMonitorHandler)
1051 {
1052     g_nwMonitorHandler = nwMonitorHandler;
1053 }
1054
1055 CAResult_t CAInitializeMessageHandler()
1056 {
1057     CASetPacketReceivedCallback(CAReceivedPacketCallback);
1058     CASetErrorHandleCallback(CAErrorHandler);
1059
1060 #ifndef SINGLE_THREAD
1061     // create thread pool
1062     CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
1063     if (CA_STATUS_OK != res)
1064     {
1065         OIC_LOG(ERROR, TAG, "thread pool initialize error.");
1066         return res;
1067     }
1068
1069     // send thread initialize
1070     res = CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
1071                                      CASendThreadProcess, CADestroyData);
1072     if (CA_STATUS_OK != res)
1073     {
1074         OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
1075         return res;
1076     }
1077
1078     // start send thread
1079     res = CAQueueingThreadStart(&g_sendThread);
1080     if (CA_STATUS_OK != res)
1081     {
1082         OIC_LOG(ERROR, TAG, "thread start error(send thread).");
1083         return res;
1084     }
1085
1086     // receive thread initialize
1087     res = CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
1088                                      CAReceiveThreadProcess, CADestroyData);
1089     if (CA_STATUS_OK != res)
1090     {
1091         OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
1092         return res;
1093     }
1094
1095 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1096     // start receive thread
1097     res = CAQueueingThreadStart(&g_receiveThread);
1098     if (CA_STATUS_OK != res)
1099     {
1100         OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
1101         return res;
1102     }
1103 #endif // SINGLE_HANDLE
1104
1105     // retransmission initialize
1106     res = CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle,
1107                                      CASendUnicastData, CATimeoutCallback, NULL);
1108     if (CA_STATUS_OK != res)
1109     {
1110         OIC_LOG(ERROR, TAG, "Failed to Initialize Retransmission.");
1111         return res;
1112     }
1113
1114 #ifdef WITH_BWT
1115     // block-wise transfer initialize
1116     res = CAInitializeBlockWiseTransfer(CAAddDataToSendThread, CAAddDataToReceiveThread);
1117     if (CA_STATUS_OK != res)
1118     {
1119         OIC_LOG(ERROR, TAG, "Failed to Initialize BlockWiseTransfer.");
1120         return res;
1121     }
1122 #endif
1123
1124     // start retransmission
1125     res = CARetransmissionStart(&g_retransmissionContext);
1126     if (CA_STATUS_OK != res)
1127     {
1128         OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
1129         return res;
1130     }
1131
1132     // initialize interface adapters by controller
1133     CAInitializeAdapters(g_threadPoolHandle);
1134 #else
1135     // retransmission initialize
1136     CAResult_t res = CARetransmissionInitialize(&g_retransmissionContext, NULL, CASendUnicastData,
1137                                                 CATimeoutCallback, NULL);
1138     if (CA_STATUS_OK != res)
1139     {
1140         OIC_LOG(ERROR, TAG, "Failed to Initialize Retransmission.");
1141         return res;
1142     }
1143
1144     CAInitializeAdapters();
1145 #endif // SINGLE_THREAD
1146
1147     return CA_STATUS_OK;
1148 }
1149
1150 void CATerminateMessageHandler()
1151 {
1152 #ifndef SINGLE_THREAD
1153     CATransportAdapter_t connType;
1154     u_arraylist_t *list = CAGetSelectedNetworkList();
1155     uint32_t length = u_arraylist_length(list);
1156
1157     uint32_t i = 0;
1158     for (i = 0; i < length; i++)
1159     {
1160         void* ptrType = u_arraylist_get(list, i);
1161
1162         if (NULL == ptrType)
1163         {
1164             continue;
1165         }
1166
1167         connType = *(CATransportAdapter_t *)ptrType;
1168         CAStopAdapter(connType);
1169     }
1170
1171     // stop retransmission
1172     if (NULL != g_retransmissionContext.threadMutex)
1173     {
1174         CARetransmissionStop(&g_retransmissionContext);
1175     }
1176
1177     // stop thread
1178     // delete thread data
1179     if (NULL != g_sendThread.threadMutex)
1180     {
1181         CAQueueingThreadStop(&g_sendThread);
1182     }
1183
1184     // stop thread
1185     // delete thread data
1186     if (NULL != g_receiveThread.threadMutex)
1187     {
1188 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1189         CAQueueingThreadStop(&g_receiveThread);
1190 #endif
1191     }
1192
1193     // destroy thread pool
1194     if (NULL != g_threadPoolHandle)
1195     {
1196         ca_thread_pool_free(g_threadPoolHandle);
1197         g_threadPoolHandle = NULL;
1198     }
1199
1200 #ifdef WITH_BWT
1201     CATerminateBlockWiseTransfer();
1202 #endif
1203     CARetransmissionDestroy(&g_retransmissionContext);
1204     CAQueueingThreadDestroy(&g_sendThread);
1205     CAQueueingThreadDestroy(&g_receiveThread);
1206
1207     // terminate interface adapters by controller
1208     CATerminateAdapters();
1209 #else
1210     // terminate interface adapters by controller
1211     CATerminateAdapters();
1212
1213     // stop retransmission
1214     CARetransmissionStop(&g_retransmissionContext);
1215     CARetransmissionDestroy(&g_retransmissionContext);
1216 #endif // SINGLE_THREAD
1217 }
1218
1219 static void CALogPayloadInfo(CAInfo_t *info)
1220 {
1221     if (info)
1222     {
1223         if (info->options)
1224         {
1225             for (uint32_t i = 0; i < info->numOptions; i++)
1226             {
1227                 OIC_LOG_V(DEBUG, TAG, "optionID: %u", info->options[i].optionID);
1228
1229                 OIC_LOG_V(DEBUG, TAG, "list: %s", info->options[i].optionData);
1230             }
1231         }
1232
1233         if (info->payload)
1234         {
1235             OIC_LOG_V(DEBUG, TAG, "payload: %p(%zu)", info->payload,
1236                       info->payloadSize);
1237         }
1238
1239         if (info->token)
1240         {
1241             OIC_LOG(DEBUG, TAG, "token:");
1242             OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) info->token,
1243                            info->tokenLength);
1244         }
1245         OIC_LOG_V(DEBUG, TAG, "msgID: %u", info->messageId);
1246     }
1247     else
1248     {
1249         OIC_LOG(DEBUG, TAG, "info is NULL, cannot output log data");
1250     }
1251 }
1252
1253 void CAErrorHandler(const CAEndpoint_t *endpoint,
1254                     const void *data, size_t dataLen,
1255                     CAResult_t result)
1256 {
1257     OIC_LOG(DEBUG, TAG, "CAErrorHandler IN");
1258     VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
1259     VERIFY_NON_NULL_VOID(data, TAG, "data");
1260
1261     if (0 == dataLen)
1262     {
1263         OIC_LOG(ERROR, TAG, "dataLen is zero");
1264         return;
1265     }
1266
1267 #ifndef SINGLE_THREAD
1268     uint32_t code = CA_NOT_FOUND;
1269     //Do not free remoteEndpoint and data. Currently they will be freed in data thread
1270     //Get PDU data
1271     coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code, endpoint);
1272     if (NULL == pdu)
1273     {
1274         OIC_LOG(ERROR, TAG, "Parse PDU failed");
1275         return;
1276     }
1277
1278     CAData_t *cadata = CAGenerateHandlerData(endpoint, NULL, pdu, CA_ERROR_DATA);
1279     if (!cadata)
1280     {
1281         OIC_LOG(ERROR, TAG, "CAErrorHandler, CAGenerateHandlerData failed!");
1282         coap_delete_pdu(pdu);
1283         return;
1284     }
1285
1286     cadata->errorInfo->result = result;
1287
1288     CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1289     coap_delete_pdu(pdu);
1290 #else
1291     (void)result;
1292 #endif
1293
1294     OIC_LOG(DEBUG, TAG, "CAErrorHandler OUT");
1295     return;
1296 }
1297
1298 static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info, CAResult_t result)
1299 {
1300     OIC_LOG(DEBUG, TAG, "CASendErrorInfo IN");
1301 #ifndef SINGLE_THREAD
1302     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
1303     if (!cadata)
1304     {
1305         OIC_LOG(ERROR, TAG, "cadata memory allocation failed");
1306         return;
1307     }
1308
1309     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
1310     if (!ep)
1311     {
1312         OIC_LOG(ERROR, TAG, "endpoint clone failed");
1313         OICFree(cadata);
1314         return;
1315     }
1316
1317     CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
1318     if (!errorInfo)
1319     {
1320         OIC_LOG(ERROR, TAG, "errorInfo memory allocation failed");
1321         OICFree(cadata);
1322         CAFreeEndpoint(ep);
1323         return;
1324     }
1325
1326     CAResult_t res = CACloneInfo(info, &errorInfo->info);
1327     if (CA_STATUS_OK != res)
1328     {
1329         OIC_LOG(ERROR, TAG, "info clone failed");
1330         OICFree(cadata);
1331         OICFree(errorInfo);
1332         CAFreeEndpoint(ep);
1333         return;
1334     }
1335
1336     errorInfo->result = result;
1337     cadata->remoteEndpoint = ep;
1338     cadata->errorInfo = errorInfo;
1339     cadata->dataType = CA_ERROR_DATA;
1340
1341     CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1342 #endif
1343     OIC_LOG(DEBUG, TAG, "CASendErrorInfo OUT");
1344 }
1345
1346 #ifndef ARDUINO
1347 static void CALogPDUInfo(const CAData_t *data, const coap_pdu_t *pdu)
1348 {
1349     OIC_LOG(DEBUG, TAG, "CALogPDUInfo");
1350
1351     VERIFY_NON_NULL_VOID(data, TAG, "data");
1352     VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
1353
1354     OIC_LOG(DEBUG, ANALYZER_TAG, "=================================================");
1355     if(SEND_TYPE_MULTICAST == data->type)
1356     {
1357         OIC_LOG(DEBUG, ANALYZER_TAG, "Is Multicast = true");
1358     }
1359     else
1360     {
1361         OIC_LOG(DEBUG, ANALYZER_TAG, "Is Multicast = false");
1362     }
1363
1364     if (NULL != data->remoteEndpoint)
1365     {
1366         CALogAdapterTypeInfo(data->remoteEndpoint->adapter);
1367         OIC_LOG(DEBUG, ANALYZER_TAG, "-------------------------------------------------");
1368         OIC_LOG_V(DEBUG, ANALYZER_TAG, "Address = [%s]:[%d]", data->remoteEndpoint->addr,
1369                   data->remoteEndpoint->port);
1370         OIC_LOG(DEBUG, ANALYZER_TAG, "-------------------------------------------------");
1371     }
1372
1373     switch(data->dataType)
1374     {
1375         case CA_REQUEST_DATA:
1376             OIC_LOG(DEBUG, ANALYZER_TAG, "Data Type = [CA_REQUEST_DATA]");
1377             break;
1378         case CA_RESPONSE_DATA:
1379             OIC_LOG(DEBUG, ANALYZER_TAG, "Data Type = [CA_RESPONSE_DATA]");
1380             break;
1381         case CA_ERROR_DATA:
1382             OIC_LOG(DEBUG, ANALYZER_TAG, "Data Type = [CA_ERROR_DATA]");
1383             break;
1384         case CA_RESPONSE_FOR_RES:
1385             OIC_LOG(DEBUG, ANALYZER_TAG, "Data Type = [CA_RESPONSE_FOR_RES]");
1386             break;
1387         default:
1388             OIC_LOG_V(DEBUG, ANALYZER_TAG, "Data Type = [%d]", data->dataType);
1389             break;
1390     }
1391     OIC_LOG(DEBUG, ANALYZER_TAG, "-------------------------------------------------");
1392
1393     const CAInfo_t *info = NULL;
1394     if (NULL != data->requestInfo)
1395     {
1396         switch(data->requestInfo->method)
1397         {
1398             case CA_GET:
1399                 OIC_LOG(DEBUG, ANALYZER_TAG, "Method = [GET]");
1400                 break;
1401             case CA_POST:
1402                 OIC_LOG(DEBUG, ANALYZER_TAG, "Method = [POST]");
1403                 break;
1404             case CA_PUT:
1405                 OIC_LOG(DEBUG, ANALYZER_TAG, "Method = [PUT]");
1406                 break;
1407             case CA_DELETE:
1408                 OIC_LOG(DEBUG, ANALYZER_TAG, "Method = [DELETE]");
1409                 break;
1410             default:
1411                 OIC_LOG_V(DEBUG, ANALYZER_TAG, "Method = [%d]", data->requestInfo->method);
1412                 break;
1413         }
1414         info = &data->requestInfo->info;
1415     }
1416
1417     if (NULL != data->responseInfo)
1418     {
1419         OIC_LOG_V(DEBUG, ANALYZER_TAG, "result code = [%d]", data->responseInfo->result);
1420         info = &data->responseInfo->info;
1421     }
1422
1423     if (pdu->transport_hdr)
1424     {
1425         OIC_LOG_V(DEBUG, ANALYZER_TAG, "Msg ID = [%d]", pdu->transport_hdr->udp.id);
1426     }
1427
1428     if (info)
1429     {
1430         OIC_LOG(DEBUG, ANALYZER_TAG, "Coap Token");
1431         OIC_LOG_BUFFER(DEBUG, ANALYZER_TAG, (const uint8_t *) info->token, info->tokenLength);
1432         OIC_LOG_V(DEBUG, ANALYZER_TAG, "Res URI = [%s]", info->resourceUri);
1433         OIC_LOG(DEBUG, ANALYZER_TAG, "-------------------------------------------------");
1434
1435         if (CA_FORMAT_APPLICATION_CBOR == info->payloadFormat)
1436         {
1437             OIC_LOG(DEBUG, ANALYZER_TAG, "Payload Format = [CA_FORMAT_APPLICATION_CBOR]");
1438         }
1439         else
1440         {
1441             OIC_LOG_V(DEBUG, ANALYZER_TAG, "Payload Format = [%d]", info->payloadFormat);
1442         }
1443     }
1444
1445     size_t payloadLen = (pdu->data) ? (unsigned char *) pdu->hdr + pdu->length - pdu->data : 0;
1446     OIC_LOG_V(DEBUG, ANALYZER_TAG, "CoAP Message Full Size = [%lu]", pdu->length);
1447     OIC_LOG(DEBUG, ANALYZER_TAG, "CoAP Header (+ 0xFF)");
1448     OIC_LOG_BUFFER(DEBUG, ANALYZER_TAG,  (const uint8_t *) pdu->transport_hdr,
1449                    pdu->length - payloadLen);
1450     OIC_LOG_V(DEBUG, ANALYZER_TAG, "CoAP Header size = [%lu]", pdu->length - payloadLen);
1451
1452     OIC_LOG_V(DEBUG, ANALYZER_TAG, "CoAP Payload");
1453     OIC_LOG_BUFFER(DEBUG, ANALYZER_TAG, pdu->data, payloadLen);
1454     OIC_LOG_V(DEBUG, ANALYZER_TAG, "CoAP Payload Size = [%lu]", payloadLen);
1455     OIC_LOG(DEBUG, ANALYZER_TAG, "=================================================");
1456 }
1457 #else
1458 static void CALogPDUInfo(const CAData_t *data, const coap_pdu_t *pdu)
1459 {
1460     VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
1461     (void)data;
1462
1463     OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
1464     OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->transport_hdr->udp.type);
1465     OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->transport_hdr->udp.code);
1466     OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
1467     OIC_LOG_BUFFER(DEBUG, TAG, pdu->transport_hdr->udp.token,
1468                    pdu->transport_hdr->udp.token_length);
1469 }
1470 #endif