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