18cb16fdec112a5177de617c6bd5ed690f6802b4
[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     uint32_t code = CA_NOT_FOUND;
741     CAData_t *cadata = NULL;
742
743     coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code,
744                                                 &(sep->endpoint));
745     if (NULL == pdu)
746     {
747         OIC_LOG(ERROR, TAG, "Parse PDU failed");
748         goto exit;
749     }
750
751     OIC_LOG_V(DEBUG, TAG, "code = %d", code);
752     if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
753     {
754         cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_REQUEST_DATA);
755         if (!cadata)
756         {
757             OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
758             coap_delete_pdu(pdu);
759             goto exit;
760         }
761     }
762     else
763     {
764         cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_RESPONSE_DATA);
765         if (!cadata)
766         {
767             OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
768             coap_delete_pdu(pdu);
769             goto exit;
770         }
771
772 #ifdef WITH_TCP
773         if (CAIsSupportedCoAPOverTCP(sep->endpoint.adapter))
774         {
775             OIC_LOG(INFO, TAG, "retransmission is not supported");
776         }
777         else
778 #endif
779         {
780             // for retransmission
781             void *retransmissionPdu = NULL;
782             CARetransmissionReceivedData(&g_retransmissionContext, cadata->remoteEndpoint, pdu->transport_hdr,
783                                          pdu->length, &retransmissionPdu);
784
785             // get token from saved data in retransmission list
786             if (retransmissionPdu && CA_EMPTY == code)
787             {
788                 if (cadata->responseInfo)
789                 {
790                     CAInfo_t *info = &cadata->responseInfo->info;
791                     CAResult_t res = CAGetTokenFromPDU((const coap_hdr_transport_t *)retransmissionPdu,
792                                                        info, &(sep->endpoint));
793                     if (CA_STATUS_OK != res)
794                     {
795                         OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
796                         OICFree(info->token);
797                         info->tokenLength = 0;
798                     }
799                 }
800             }
801             OICFree(retransmissionPdu);
802         }
803     }
804
805     cadata->type = SEND_TYPE_UNICAST;
806
807     CALogPDUInfo(cadata, pdu);
808
809 #ifdef SINGLE_THREAD
810     CAProcessReceivedData(cadata);
811 #else
812 #ifdef WITH_BWT
813     if (CAIsSupportedBlockwiseTransfer(sep->endpoint.adapter))
814     {
815         CAResult_t res = CAReceiveBlockWiseData(pdu, &(sep->endpoint), cadata, dataLen);
816         if (CA_NOT_SUPPORTED == res || CA_REQUEST_TIMEOUT == res)
817         {
818             OIC_LOG(DEBUG, TAG, "this message does not have block option");
819             CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
820         }
821         else
822         {
823             CADestroyData(cadata, sizeof(CAData_t));
824         }
825     }
826     else
827 #endif
828     {
829         CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
830     }
831 #endif // SINGLE_THREAD
832
833     coap_delete_pdu(pdu);
834
835 exit:
836     OIC_LOG(DEBUG, TAG, "received pdu data :");
837     OIC_LOG_BUFFER(DEBUG, TAG,  data, dataLen);
838 }
839
840 void CAHandleRequestResponseCallbacks()
841 {
842 #ifdef SINGLE_THREAD
843     CAReadData();
844     CARetransmissionBaseRoutine((void *)&g_retransmissionContext);
845 #else
846 #ifdef SINGLE_HANDLE
847     // parse the data and call the callbacks.
848     // #1 parse the data
849     // #2 get endpoint
850
851     oc_mutex_lock(g_receiveThread.threadMutex);
852
853     u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
854
855     oc_mutex_unlock(g_receiveThread.threadMutex);
856
857     if (NULL == item || NULL == item->msg)
858     {
859         return;
860     }
861
862     // get endpoint
863     CAData_t *td = (CAData_t *) item->msg;
864
865     if (td->requestInfo && g_requestHandler)
866     {
867         OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
868         g_requestHandler(td->remoteEndpoint, td->requestInfo);
869     }
870     else if (td->responseInfo && g_responseHandler)
871     {
872         OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
873         g_responseHandler(td->remoteEndpoint, td->responseInfo);
874     }
875     else if (td->errorInfo && g_errorHandler)
876     {
877         OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
878         g_errorHandler(td->remoteEndpoint, td->errorInfo);
879     }
880
881     CADestroyData(item->msg, sizeof(CAData_t));
882     OICFree(item);
883
884 #endif // SINGLE_HANDLE
885 #endif // SINGLE_THREAD
886 }
887
888 static CAData_t* CAPrepareSendData(const CAEndpoint_t *endpoint, const void *sendData,
889                                    CADataType_t dataType)
890 {
891     OIC_LOG(DEBUG, TAG, "CAPrepareSendData IN");
892
893     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
894     if (!cadata)
895     {
896         OIC_LOG(ERROR, TAG, "memory allocation failed");
897         return NULL;
898     }
899
900     if (CA_REQUEST_DATA == dataType)
901     {
902 #ifdef SINGLE_THREAD
903         CARequestInfo_t *request = (CARequestInfo_t *)sendData;
904 #else
905         // clone request info
906         CARequestInfo_t *request = CACloneRequestInfo((CARequestInfo_t *)sendData);
907         if (!request)
908         {
909             OIC_LOG(ERROR, TAG, "CACloneRequestInfo failed");
910             goto exit;
911         }
912 #endif
913         cadata->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
914         cadata->requestInfo =  request;
915     }
916     else if (CA_RESPONSE_DATA == dataType || CA_RESPONSE_FOR_RES == dataType)
917     {
918 #ifdef SINGLE_THREAD
919         CAResponseInfo_t *response = (CAResponseInfo_t *)sendData;
920 #else
921         // clone response info
922         CAResponseInfo_t *response = CACloneResponseInfo((CAResponseInfo_t *)sendData);
923         if(!response)
924         {
925             OIC_LOG(ERROR, TAG, "CACloneResponseInfo failed");
926             goto exit;
927         }
928 #endif
929         cadata->type = response->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
930         cadata->responseInfo = response;
931     }
932     else
933     {
934         OIC_LOG(ERROR, TAG, "CAPrepareSendData unknown data type");
935         goto exit;
936     }
937
938 #ifdef SINGLE_THREAD
939     CAEndpoint_t* ep = endpoint;
940 #else
941     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
942     if (!ep)
943     {
944         OIC_LOG(ERROR, TAG, "endpoint clone failed");
945         goto exit;
946     }
947 #endif
948     cadata->remoteEndpoint = ep;
949     cadata->dataType = dataType;
950     return cadata;
951
952 exit:
953 #ifndef SINGLE_THREAD
954     CADestroyData(cadata, sizeof(CAData_t));
955 #else
956     OICFree(cadata);
957 #endif
958     return NULL;
959 }
960
961 CAResult_t CADetachSendMessage(const CAEndpoint_t *endpoint, const void *sendMsg,
962                                CADataType_t dataType)
963 {
964     VERIFY_NON_NULL(endpoint, TAG, "endpoint");
965     VERIFY_NON_NULL(sendMsg, TAG, "sendMsg");
966
967     if (false == CAIsSelectedNetworkAvailable())
968     {
969         return CA_STATUS_FAILED;
970     }
971
972 #ifdef ARDUINO
973     // If max retransmission queue is reached, then don't handle new request
974     if (CA_MAX_RT_ARRAY_SIZE == u_arraylist_length(g_retransmissionContext.dataList))
975     {
976         OIC_LOG(ERROR, TAG, "max RT queue size reached!");
977         return CA_SEND_FAILED;
978     }
979 #endif // ARDUINO
980
981     CAData_t *data = CAPrepareSendData(endpoint, sendMsg, dataType);
982     if(!data)
983     {
984         OIC_LOG(ERROR, TAG, "CAPrepareSendData failed");
985         return CA_MEMORY_ALLOC_FAILED;
986     }
987
988     OIC_LOG_V(DEBUG, TAG, "device ID of endpoint of this message is %s", endpoint->deviceId);
989
990 #ifdef SINGLE_THREAD
991     CAResult_t result = CAProcessSendData(data);
992     if (CA_STATUS_OK != result)
993     {
994         OIC_LOG(ERROR, TAG, "CAProcessSendData failed");
995         OICFree(data);
996         return result;
997     }
998
999     OICFree(data);
1000
1001 #else
1002     if (SEND_TYPE_UNICAST == data->type && CAIsLocalEndpoint(data->remoteEndpoint))
1003     {
1004         OIC_LOG(DEBUG, TAG,
1005                 "This is a loopback message. Transfer it to the receive queue directly");
1006         CAQueueingThreadAddData(&g_receiveThread, data, sizeof(CAData_t));
1007         return CA_STATUS_OK;
1008     }
1009 #ifdef WITH_BWT
1010     if (CAIsSupportedBlockwiseTransfer(endpoint->adapter))
1011     {
1012         // send block data
1013         CAResult_t res = CASendBlockWiseData(data);
1014         if (CA_NOT_SUPPORTED == res)
1015         {
1016             OIC_LOG(DEBUG, TAG, "normal msg will be sent");
1017             CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
1018             return CA_STATUS_OK;
1019         }
1020         else
1021         {
1022             CADestroyData(data, sizeof(CAData_t));
1023         }
1024         return res;
1025     }
1026     else
1027 #endif // WITH_BWT
1028     {
1029         CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
1030     }
1031 #endif // SINGLE_THREAD
1032
1033     return CA_STATUS_OK;
1034 }
1035
1036 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
1037                              CAErrorCallback errorHandler)
1038 {
1039     g_requestHandler = ReqHandler;
1040     g_responseHandler = RespHandler;
1041     g_errorHandler = errorHandler;
1042 }
1043
1044 void CASetNetworkMonitorCallback(CANetworkMonitorCallback nwMonitorHandler)
1045 {
1046     g_nwMonitorHandler = nwMonitorHandler;
1047 }
1048
1049 CAResult_t CAInitializeMessageHandler()
1050 {
1051     CASetPacketReceivedCallback(CAReceivedPacketCallback);
1052     CASetErrorHandleCallback(CAErrorHandler);
1053
1054 #ifndef SINGLE_THREAD
1055     // create thread pool
1056     CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
1057     if (CA_STATUS_OK != res)
1058     {
1059         OIC_LOG(ERROR, TAG, "thread pool initialize error.");
1060         return res;
1061     }
1062
1063     // send thread initialize
1064     res = CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
1065                                      CASendThreadProcess, CADestroyData);
1066     if (CA_STATUS_OK != res)
1067     {
1068         OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
1069         return res;
1070     }
1071
1072     // start send thread
1073     res = CAQueueingThreadStart(&g_sendThread);
1074     if (CA_STATUS_OK != res)
1075     {
1076         OIC_LOG(ERROR, TAG, "thread start error(send thread).");
1077         return res;
1078     }
1079
1080     // receive thread initialize
1081     res = CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
1082                                      CAReceiveThreadProcess, CADestroyData);
1083     if (CA_STATUS_OK != res)
1084     {
1085         OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
1086         return res;
1087     }
1088
1089 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1090     // start receive thread
1091     res = CAQueueingThreadStart(&g_receiveThread);
1092     if (CA_STATUS_OK != res)
1093     {
1094         OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
1095         return res;
1096     }
1097 #endif // SINGLE_HANDLE
1098
1099     // retransmission initialize
1100     res = CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle,
1101                                      CASendUnicastData, CATimeoutCallback, NULL);
1102     if (CA_STATUS_OK != res)
1103     {
1104         OIC_LOG(ERROR, TAG, "Failed to Initialize Retransmission.");
1105         return res;
1106     }
1107
1108 #ifdef WITH_BWT
1109     // block-wise transfer initialize
1110     res = CAInitializeBlockWiseTransfer(CAAddDataToSendThread, CAAddDataToReceiveThread);
1111     if (CA_STATUS_OK != res)
1112     {
1113         OIC_LOG(ERROR, TAG, "Failed to Initialize BlockWiseTransfer.");
1114         return res;
1115     }
1116 #endif
1117
1118     // start retransmission
1119     res = CARetransmissionStart(&g_retransmissionContext);
1120     if (CA_STATUS_OK != res)
1121     {
1122         OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
1123         return res;
1124     }
1125
1126     // initialize interface adapters by controller
1127     CAInitializeAdapters(g_threadPoolHandle);
1128 #else
1129     // retransmission initialize
1130     CAResult_t res = CARetransmissionInitialize(&g_retransmissionContext, NULL, CASendUnicastData,
1131                                                 CATimeoutCallback, NULL);
1132     if (CA_STATUS_OK != res)
1133     {
1134         OIC_LOG(ERROR, TAG, "Failed to Initialize Retransmission.");
1135         return res;
1136     }
1137
1138     CAInitializeAdapters();
1139 #endif // SINGLE_THREAD
1140
1141     return CA_STATUS_OK;
1142 }
1143
1144 void CATerminateMessageHandler()
1145 {
1146 #ifndef SINGLE_THREAD
1147     CATransportAdapter_t connType;
1148     u_arraylist_t *list = CAGetSelectedNetworkList();
1149     uint32_t length = u_arraylist_length(list);
1150
1151     uint32_t i = 0;
1152     for (i = 0; i < length; i++)
1153     {
1154         void* ptrType = u_arraylist_get(list, i);
1155
1156         if (NULL == ptrType)
1157         {
1158             continue;
1159         }
1160
1161         connType = *(CATransportAdapter_t *)ptrType;
1162         CAStopAdapter(connType);
1163     }
1164
1165     // stop retransmission
1166     if (NULL != g_retransmissionContext.threadMutex)
1167     {
1168         CARetransmissionStop(&g_retransmissionContext);
1169     }
1170
1171     // stop thread
1172     // delete thread data
1173     if (NULL != g_sendThread.threadMutex)
1174     {
1175         CAQueueingThreadStop(&g_sendThread);
1176     }
1177
1178     // stop thread
1179     // delete thread data
1180     if (NULL != g_receiveThread.threadMutex)
1181     {
1182 #ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
1183         CAQueueingThreadStop(&g_receiveThread);
1184 #endif
1185     }
1186
1187     // destroy thread pool
1188     if (NULL != g_threadPoolHandle)
1189     {
1190         ca_thread_pool_free(g_threadPoolHandle);
1191         g_threadPoolHandle = NULL;
1192     }
1193
1194 #ifdef WITH_BWT
1195     CATerminateBlockWiseTransfer();
1196 #endif
1197     CARetransmissionDestroy(&g_retransmissionContext);
1198     CAQueueingThreadDestroy(&g_sendThread);
1199     CAQueueingThreadDestroy(&g_receiveThread);
1200
1201     // terminate interface adapters by controller
1202     CATerminateAdapters();
1203 #else
1204     // terminate interface adapters by controller
1205     CATerminateAdapters();
1206
1207     // stop retransmission
1208     CARetransmissionStop(&g_retransmissionContext);
1209     CARetransmissionDestroy(&g_retransmissionContext);
1210 #endif // SINGLE_THREAD
1211 }
1212
1213 static void CALogPayloadInfo(CAInfo_t *info)
1214 {
1215     if (info)
1216     {
1217         if (info->options)
1218         {
1219             for (uint32_t i = 0; i < info->numOptions; i++)
1220             {
1221                 OIC_LOG_V(DEBUG, TAG, "optionID: %u", info->options[i].optionID);
1222
1223                 OIC_LOG_V(DEBUG, TAG, "list: %s", info->options[i].optionData);
1224             }
1225         }
1226
1227         if (info->payload)
1228         {
1229             OIC_LOG_V(DEBUG, TAG, "payload: %p(%zu)", info->payload,
1230                       info->payloadSize);
1231         }
1232
1233         if (info->token)
1234         {
1235             OIC_LOG(DEBUG, TAG, "token:");
1236             OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) info->token,
1237                            info->tokenLength);
1238         }
1239         OIC_LOG_V(DEBUG, TAG, "msgID: %u", info->messageId);
1240     }
1241     else
1242     {
1243         OIC_LOG(DEBUG, TAG, "info is NULL, cannot output log data");
1244     }
1245 }
1246
1247 void CAErrorHandler(const CAEndpoint_t *endpoint,
1248                     const void *data, size_t dataLen,
1249                     CAResult_t result)
1250 {
1251     OIC_LOG(DEBUG, TAG, "CAErrorHandler IN");
1252
1253 #ifndef SINGLE_THREAD
1254     VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
1255     VERIFY_NON_NULL_VOID(data, TAG, "data");
1256
1257     uint32_t code = CA_NOT_FOUND;
1258     //Do not free remoteEndpoint and data. Currently they will be freed in data thread
1259     //Get PDU data
1260     coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code, endpoint);
1261     if (NULL == pdu)
1262     {
1263         OIC_LOG(ERROR, TAG, "Parse PDU failed");
1264         return;
1265     }
1266
1267     CAData_t *cadata = CAGenerateHandlerData(endpoint, NULL, pdu, CA_ERROR_DATA);
1268     if (!cadata)
1269     {
1270         OIC_LOG(ERROR, TAG, "CAErrorHandler, CAGenerateHandlerData failed!");
1271         coap_delete_pdu(pdu);
1272         return;
1273     }
1274
1275     cadata->errorInfo->result = result;
1276
1277     CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1278     coap_delete_pdu(pdu);
1279 #endif
1280
1281     OIC_LOG(DEBUG, TAG, "CAErrorHandler OUT");
1282     return;
1283 }
1284
1285 static void CASendErrorInfo(const CAEndpoint_t *endpoint, const CAInfo_t *info, CAResult_t result)
1286 {
1287     OIC_LOG(DEBUG, TAG, "CASendErrorInfo IN");
1288 #ifndef SINGLE_THREAD
1289     CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
1290     if (!cadata)
1291     {
1292         OIC_LOG(ERROR, TAG, "cadata memory allocation failed");
1293         return;
1294     }
1295
1296     CAEndpoint_t* ep = CACloneEndpoint(endpoint);
1297     if (!ep)
1298     {
1299         OIC_LOG(ERROR, TAG, "endpoint clone failed");
1300         OICFree(cadata);
1301         return;
1302     }
1303
1304     CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
1305     if (!errorInfo)
1306     {
1307         OIC_LOG(ERROR, TAG, "errorInfo memory allocation failed");
1308         OICFree(cadata);
1309         CAFreeEndpoint(ep);
1310         return;
1311     }
1312
1313     CAResult_t res = CACloneInfo(info, &errorInfo->info);
1314     if (CA_STATUS_OK != res)
1315     {
1316         OIC_LOG(ERROR, TAG, "info clone failed");
1317         OICFree(cadata);
1318         OICFree(errorInfo);
1319         CAFreeEndpoint(ep);
1320         return;
1321     }
1322
1323     errorInfo->result = result;
1324     cadata->remoteEndpoint = ep;
1325     cadata->errorInfo = errorInfo;
1326     cadata->dataType = CA_ERROR_DATA;
1327
1328     CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
1329 #endif
1330     OIC_LOG(DEBUG, TAG, "CASendErrorInfo OUT");
1331 }
1332
1333 #ifndef ARDUINO
1334 static void CALogPDUInfo(const CAData_t *data, const coap_pdu_t *pdu)
1335 {
1336     OIC_LOG(DEBUG, TAG, "CALogPDUInfo");
1337
1338     VERIFY_NON_NULL_VOID(data, TAG, "data");
1339     VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
1340
1341     OIC_LOG(DEBUG, ANALYZER_TAG, "=================================================");
1342     if(SEND_TYPE_MULTICAST == data->type)
1343     {
1344         OIC_LOG(DEBUG, ANALYZER_TAG, "Is Multicast = true");
1345     }
1346     else
1347     {
1348         OIC_LOG(DEBUG, ANALYZER_TAG, "Is Multicast = false");
1349     }
1350
1351     if (NULL != data->remoteEndpoint)
1352     {
1353         CALogAdapterTypeInfo(data->remoteEndpoint->adapter);
1354         OIC_LOG(DEBUG, ANALYZER_TAG, "-------------------------------------------------");
1355         OIC_LOG_V(DEBUG, ANALYZER_TAG, "Address = [%s]:[%d]", data->remoteEndpoint->addr,
1356                   data->remoteEndpoint->port);
1357         OIC_LOG(DEBUG, ANALYZER_TAG, "-------------------------------------------------");
1358     }
1359
1360     switch(data->dataType)
1361     {
1362         case CA_REQUEST_DATA:
1363             OIC_LOG(DEBUG, ANALYZER_TAG, "Data Type = [CA_REQUEST_DATA]");
1364             break;
1365         case CA_RESPONSE_DATA:
1366             OIC_LOG(DEBUG, ANALYZER_TAG, "Data Type = [CA_RESPONSE_DATA]");
1367             break;
1368         case CA_ERROR_DATA:
1369             OIC_LOG(DEBUG, ANALYZER_TAG, "Data Type = [CA_ERROR_DATA]");
1370             break;
1371         case CA_RESPONSE_FOR_RES:
1372             OIC_LOG(DEBUG, ANALYZER_TAG, "Data Type = [CA_RESPONSE_FOR_RES]");
1373             break;
1374         default:
1375             OIC_LOG_V(DEBUG, ANALYZER_TAG, "Data Type = [%d]", data->dataType);
1376             break;
1377     }
1378     OIC_LOG(DEBUG, ANALYZER_TAG, "-------------------------------------------------");
1379
1380     const CAInfo_t *info = NULL;
1381     if (NULL != data->requestInfo)
1382     {
1383         switch(data->requestInfo->method)
1384         {
1385             case CA_GET:
1386                 OIC_LOG(DEBUG, ANALYZER_TAG, "Method = [GET]");
1387                 break;
1388             case CA_POST:
1389                 OIC_LOG(DEBUG, ANALYZER_TAG, "Method = [POST]");
1390                 break;
1391             case CA_PUT:
1392                 OIC_LOG(DEBUG, ANALYZER_TAG, "Method = [PUT]");
1393                 break;
1394             case CA_DELETE:
1395                 OIC_LOG(DEBUG, ANALYZER_TAG, "Method = [DELETE]");
1396                 break;
1397             default:
1398                 OIC_LOG_V(DEBUG, ANALYZER_TAG, "Method = [%d]", data->requestInfo->method);
1399                 break;
1400         }
1401         info = &data->requestInfo->info;
1402     }
1403
1404     if (NULL != data->responseInfo)
1405     {
1406         OIC_LOG_V(DEBUG, ANALYZER_TAG, "result code = [%d]", data->responseInfo->result);
1407         info = &data->responseInfo->info;
1408     }
1409
1410     if (pdu->transport_hdr)
1411     {
1412         OIC_LOG_V(DEBUG, ANALYZER_TAG, "Msg ID = [%d]", pdu->transport_hdr->udp.id);
1413     }
1414
1415     if (info)
1416     {
1417         OIC_LOG(DEBUG, ANALYZER_TAG, "Coap Token");
1418         OIC_LOG_BUFFER(DEBUG, ANALYZER_TAG, (const uint8_t *) info->token, info->tokenLength);
1419         OIC_LOG_V(DEBUG, ANALYZER_TAG, "Res URI = [%s]", info->resourceUri);
1420         OIC_LOG(DEBUG, ANALYZER_TAG, "-------------------------------------------------");
1421
1422         if (CA_FORMAT_APPLICATION_CBOR == info->payloadFormat)
1423         {
1424             OIC_LOG(DEBUG, ANALYZER_TAG, "Payload Format = [CA_FORMAT_APPLICATION_CBOR]");
1425         }
1426         else
1427         {
1428             OIC_LOG_V(DEBUG, ANALYZER_TAG, "Payload Format = [%d]", info->payloadFormat);
1429         }
1430     }
1431
1432     size_t payloadLen = (pdu->data) ? (unsigned char *) pdu->hdr + pdu->length - pdu->data : 0;
1433     OIC_LOG_V(DEBUG, ANALYZER_TAG, "CoAP Message Full Size = [%lu]", pdu->length);
1434     OIC_LOG(DEBUG, ANALYZER_TAG, "CoAP Header (+ 0xFF)");
1435     OIC_LOG_BUFFER(DEBUG, ANALYZER_TAG,  (const uint8_t *) pdu->transport_hdr,
1436                    pdu->length - payloadLen);
1437     OIC_LOG_V(DEBUG, ANALYZER_TAG, "CoAP Header size = [%lu]", pdu->length - payloadLen);
1438
1439     OIC_LOG_V(DEBUG, ANALYZER_TAG, "CoAP Payload");
1440     OIC_LOG_BUFFER(DEBUG, ANALYZER_TAG, pdu->data, payloadLen);
1441     OIC_LOG_V(DEBUG, ANALYZER_TAG, "CoAP Payload Size = [%lu]", payloadLen);
1442     OIC_LOG(DEBUG, ANALYZER_TAG, "=================================================");
1443 }
1444 #else
1445 static void CALogPDUInfo(const CAData_t *data, const coap_pdu_t *pdu)
1446 {
1447     VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
1448     (void)data;
1449
1450     OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
1451     OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->transport_hdr->udp.type);
1452     OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->transport_hdr->udp.code);
1453     OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
1454     OIC_LOG_BUFFER(DEBUG, TAG, pdu->transport_hdr->udp.token,
1455                    pdu->transport_hdr->udp.token_length);
1456 }
1457 #endif